• PieMePlenty@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      1 day ago

      It does to some degree.

      • “11” is string, 1 is an int, because strings can be added (+) convert int to string and combine: “11”+“1” = “111”
      • “11” is string, 1 is an int, because strings cant be subtracted (-) convert string to int and combine: 11-1 = 10

      I’m not into JS so I don’t know how it takes priority. ints can be added too, so I guess its basing it on the first variable which is compatible with the operator: in the first case string, in the second case int.

      If this is how it works, it makes sense. But imo its a case of the designers being preoccupied with whether or not they could, they didn’t stop to think if they should.

  • bitjunkie@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    2 days ago

    It’s because + is two different operators and overloads based on the type to the left, while - is only a numeric operator and coerces left and right operands to numeric. But frankly if you’re still using + for math or string concatenation in 2025, you’re doing it wrong.

    • Hadriscus@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      I know nothing about javascript, what is wrong with using + for math? perhaps naively, I’d say it looks suited for the job

        • bitjunkie@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          2 days ago

          Point taken but the one I use is only ~200k for the whole package, ~11k for the actual file that gets loaded

      • Quibblekrust@thelemmy.club
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 days ago

        It’s much better to make your own function that uses bitwise operations to do addition.

        function add(a, b) {
            while (b !== 0) {
                // Calculate carry
                let carry = a & b;
        
                // Sum without carry
                a = a ^ b;
        
                // Shift carry to the left
                b = carry << 1;
            }
            return a;
        }
        

        (For certain definitions of better.)

  • arc@lemm.ee
    link
    fedilink
    arrow-up
    0
    ·
    2 days ago

    Javascript is a dogshit language that everyone is stuck with. The best that we can hope for is the likes of typescript take the edge off of it. Even though it’s like smearing marzipan over a turd.

    • Fijxu@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      JS should have never leaved the Browser side. Now you can use this thing for Backend and is just awful

  • kamen@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 days ago

    If you’re consciously and intentionally using JavaScript like that, I don’t want to be friends with you.

    • zagaberoo@sopuli.xyz
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly ‘helpful’ in assuming intent.

          • FooBarrington@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            2 days ago

            Not really, considering Typescript only adds static types to JS. It’s not a different language, it’s an extension.

            • Quibblekrust@thelemmy.club
              link
              fedilink
              English
              arrow-up
              0
              ·
              2 days ago

              Since it needs to be compiled to JavaScript in order to be used, I kind of consider it a different language. Yes, it’s a strict superset of JavaScript, but that makes it different.

              • FooBarrington@lemmy.world
                link
                fedilink
                arrow-up
                0
                ·
                edit-2
                2 days ago

                That’s your prerogative, but it honestly doesn’t make sense. Typescript adds almost no functionality to JS (and the few pieces it adds are now considered mistakes that shouldn’t be used anymore). It only focuses on adding typing information, and in the future you’ll be able to run TS that doesn’t use those few added features as JS (see the proposal).

                You can also add the TS types as comments in your JS code, which IMO shows that it’s not a different language.

  • proctor1432@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 days ago

    Heck, I need to learn some new languages apparently. Here I was expecting an angry "CS0029 cannot implicitly convert type ‘string’ to ‘int’!

  • capybara@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 days ago

    To start off… Using arithmetic operators on strings in combination with integers is a pure skill issue. Let’s disregard this.

    If you were to use + where one part is a string, it’s natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it’s safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.

    This is an issue with untyped languages. If you don’t like it, use typescript. End of story.

    • Jankatarch@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      Instead of trying to make it work, javascript could just say “error.” Being untyped doesn’t mean you can’t have error messages.

      • bss03@infosec.pub
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 days ago

        I think it’s less about type system, and more about lack of a separate compilation step.

        With a compilation step, you can have error messages that developers see, but users don’t. (Hopefully, these errors enable the developers to reduce the errors that users see, and just generally improve the UX, but that’s NOT guaranteed.)

        Without a compilation step, you have to assign some semantics to whatever random source string your interpreter gets. And, while you can certainly make that an error, that would rarely be helpful for the user. JS instead made the choice to, as much as possible, avoid error semantics in favor of silent coercions, conversions, and conflations in order to make every attempt to not “error-out” on the user.

        It would be a very painful decade indeed to now change the semantics for some JS source text.

        Purescript is a great option. Typescript is okay. You could also introduce a JS-to-JS “compilation” step that DID reject (or at least warn the developer) for source text that “should” be given an error semantic, but I don’t know an “off-the-shelf” approach for that – other than JSLint.

      • capybara@lemm.ee
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 days ago

        This is fair enough from an idealistic view. In practice, you don’t want your entire website to shit itself because of a potentially insignificant error.

        • Valmond@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          2 days ago

          Look! I bought this for free on capybaras website, there’s a glitch!

          capybara: at least it didn’t throw an error.

          / jk 😁

        • Kacarott@aussie.zone
          link
          fedilink
          arrow-up
          0
          ·
          2 days ago

          This is exactly why it should throw an error, to make it incredibly obvious something isn’t working correctly so it can be fixed. Otherwise you have wrong logic leading to hard to notice and hard to debug problems in your code

            • TheBeege@lemmy.world
              link
              fedilink
              arrow-up
              0
              ·
              edit-2
              2 days ago

              No. I don’t want to transpile. I don’t want a bundle. I want a simple site that works in the browser. I want to serve it as a static site. I don’t want a build step. I don’t want node_modules. I want to code using the language targeted for the platform without any other nonsense.

              Javascript is cancer. Fucking left pad?! How the fuck did we let that happen? What is this insane fucking compulsion to have libraries for two lines of code? To need configuration after configuration just to run fucking hello world with types and linting?

              No, fuck Typescript. Microsoft owns enough. They own where you store your code. They own your IDE. They might own your operating system. Too much in one place. They don’t need to own the language I use, too.

              “Let’s use a proprietary improvement to fix the standard that should have not sucked in the first place” is why we can’t have nice things.

              No.

  • bss03@infosec.pub
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 days ago

    This is my favorite language: GHC Haskell

    GHC Haskell:

    GHCi> length (2, 'foo')
    1
    
    • yetAnotherUser@lemmy.ca
      link
      fedilink
      arrow-up
      0
      ·
      2 days ago

      Wait, now I need to know why.

      * some time later *

      I went to check why the hell this happened. It looks like the pair (“(,)”) is defined as an instance of Foldable, for some reason, which is the class used by functions like foldl() and foldr(). Meanwhile, triples and other tuples of higher order (such as triples, quadruples, …) are not instances of Foldable.

      The weirdest part is that, if you try to use a pair as a Foldable, you only get the second value, for some reason… Here is an example.

      ghci> foldl (\acc x -> x:acc) [] (1,2)
      
      [2]
      

      This makes it so that the returned length is 1.

        • bss03@infosec.pub
          link
          fedilink
          English
          arrow-up
          0
          ·
          2 days ago

          (.) is a valid expression in Haskell. Normally it is the prefix form of the infix operator . that does function composition. (.) (2*) (1+) 3 = ((2*) . (1+)) 3 = 2 * (1 + 3) = 8.

          But, the most common use of the word “boob” in my experience in Haskell is the “boobs operator”: (.)(.). It’s usage in Haskell is limited (tho valid), but it’s appearance in racy ASCII art predates even the first versions on Haskell.

      • bss03@infosec.pub
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 days ago

        Oddly enough, in Haskell (as defined by the report), length is monomorphic, so it just doesn’t work on tuples (type error).

        Due to the way kinds (types of types) work in Haskell, Foldable instances can only operate over (i.e. length only counts) elements of the last/final type argument. So, for (,) it only counts the second part, which is always there exactly once. If you provided a Foldable for (,) it would also have length of 1.

  • Destide@feddit.uk
    link
    fedilink
    English
    arrow-up
    0
    ·
    3 days ago

    Oh we’ve hit an issue that’s solved by another language or we could make another framework