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

    That’s the case in many languages, pretty much in all that don’t have a separate string concatenation operator.

    • squaresinger@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      2 days ago

      Yeah, and almost all languages I know then would throw an exception when you try to use - with a string, and if they offer multiple operators that take a string and a number, they always only perform string operations with that and never cast to a number type to do math operations with it.

      (e.g. some languages have + for string concatenation and * to add the same string X time together, so e.g. "ab" * 2 => "abab". It’s a terrible idea to have + perform a string operation and - performs a math operation.)

        • squaresinger@lemmy.world
          link
          fedilink
          arrow-up
          0
          arrow-down
          1
          ·
          2 days ago

          Because there’s in fact no operator overloading happening, true, but that’s mostly an under-the-hood topic.

          It should not happen no matter why it does happen under the hood.

          Operator overloading for string - string is wrong and type coercion to implicitly cast this to int(string) - int(string) is just as wrong.

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

            There is operator overloading happening - the + operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.

            It should not happen no matter why it does happen under the hood.

            If you don’t want it to happen either use a different language, or ensure you don’t run into this case (e.g. by using Typescript). It’s an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.

            • squaresinger@lemmy.world
              link
              fedilink
              arrow-up
              2
              arrow-down
              1
              ·
              2 days ago

              There is operator overloading happening - the + operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.

              For string + string and number + number there is operator overloading, that’s correct. For string + number there is not, there’s only type coercion. It becomes string + string(number). All of that is fine. Other languages do that as well.

              What’s not fine is that JS also looks the other way on the type coercion tree: There’s no string - string overloading, so it goes down the type coercion tree, looking for any - operation that it can cast to and it ends up with number(string) - number(string), which makes no sense at all.

              If you don’t want it to happen either use a different language, or ensure you don’t run into this case (e.g. by using Typescript). It’s an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.

              It’s not the point of the discussion that there are other languages that are better. This here is about complaining about bad language design, and no matter how you turn this, this is not a matter of taste or anything, this is just bad language design.

              You are obviously right that this crap will stay in JS forever. That doesn’t make it good design.