• hperrin@lemmy.ca
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 hour ago

    I mean how can you define a sensible way to subtract Infinity from an array, or add an object to a string? The way JavaScript defines it is predictable, easy to compute, and handles bad code gracefully, which is a good tradeoff between doing something like matrix arithmetic on a CPU and just straight up crashing. If you’re doing silly things like that, you should know how JavaScript coerces types, but I don’t do silly things like that, so I don’t really care how JavaScript would handle it. Every language will do silly things if you force it to. That doesn’t make it a bad language.

    Do you feel the same about C because C lets you take pointers of pointers of pointers until you’re addressing random memory in an unpredictable way? No, because it’s silly to do that.

    • FishFace@piefed.social
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 hour ago

      I mean how can you define a sensible way to subtract Infinity from an array, or add an object to a string?

      TypeError.

      There are also various sensible ways, for example if you have an array of floats, subtracting Infinity from the array could result in an array of the same length as the original, with each value being negative Infinity. But in general inhomogeneous types should not be addable without careful thought to create a type system which is consistent and coherent. That is exactly what JavaScript did not do.

      It doesn’t “handle bad code gracefully”; it handles it in a way that’s hard to reason about and hence hard to work with.

      The way JavaScript defines it is predictable

      You literally just failed to predict it, so I don’t think there’s any point continuing this conversation.

      • hperrin@lemmy.ca
        link
        fedilink
        English
        arrow-up
        1
        ·
        3 minutes ago

        Ok, except I did predict it. It turns them both into strings and gives you “12”. I checked it.

        So, should web pages be prone to crashing if everything isn’t perfect? I don’t know if you remember XHTML, but that was basically what happened with that. You have a “div” within a “p”? Page crashed. You have an unclosed “span”? Page crashed. XHTML was abandoned because is constantly broke the web.

        Web technologies are supposed to be resilient, so throwing TypeError is the last resort for something that absolutely cannot work, like trying to add to a Symbol. Since nothing from the user is ever a Symbol (there’s no input that can give it, and it can’t be stored in JSON), it’s acceptable to throw a TypeError there.

        JavaScript is meant to be fast and resilient. Its type conversions make sense when you consider those goals.