5 ms·
> Want to concat an number and string ? JavaScript wont complain. Is that supposed to be an endorsement?
by wyager 10y ago
> Want to concat an number and string ? JavaScript wont complain.
Is that supposed to be an endorsement?
- z3t4 10y agofoo = (string, number) => string + number Now go write that in your preferred language!
- wyager 10y agoWhy would I want to? Better to explicitly convert the number to a string.
- z3t4 10y agoThis is something you do often in JavaScript, for example: "You have " + messages + " new messages"
- kaoD 10y ago"You have undefined new messages" Glorious!
- z3t4 10y agoIt's very easy to spot the error here, compared to for example a segfault.
- lmm 10y agoThat's damning with faint praise if I ever heard it. (And it's not even true; undefined tends to propagate further, the segfault usually happens closer to the actual error. Not that I'm defending languages that segfault by any means)
- kaoD 10y agoNot to mention a SEGFAULT in Rust is a bug in Rust the language/std, not your program (unless you're writing unsafe code, which is pretty much never).
- wyager 10y ago"You have " ++ show messages ++ "new messages" This is type safe.
- colemickens 10y agoSo, since we're talking about Rust... print!("You have %d new messages", messages) Was that supposed to make me want to abandon all type safety and embrace a GC'd language that runs in a VM?
- z3t4 10y agoThere are advantages to GC, even Rust has runtime GC. But where is the memory freed ? You don't have to think of that at all in JavaScript. Also when something goes wrong with types in JavaScript the worst case scenario is "2"+2 turns into "22" witch is easy to avoid, compared to a silent overwrite/overflow. Even if the types in JavaScript is very loose, they are much safer.
- lmm 10y ago> There are advantages to GC, even Rust has runtime GC. If you want GC there are any number of good languages to pick with it (e.g. OCaml). > Also when something goes wrong with types in JavaScript the worst case scenario is "2"+2 turns into "22" witch is easy to avoid, compared to a silent overwrite/overflow. It's memory-safe but it's not safe. What if an error like that happens in your permission-checking code? I agree that silent overwrites and silent overflows should not happen and that language that have those things are bad, but that doesn't make silent type errors any better.
- z3t4 10y agoThe problem here is that + is used for both addition and concatenation. Most of the time you know what you are doing though (smile) and string is the default. When I started out with JavaScript I used -- (minus minus) for addition just to be safe. If you where to compare "22" == 22 ? it would be true, so the type doesn't really matter. If you want to do arithmetics on a string, no problem! Even null behaves like a teddy bear compared to other languages where it can bite you hard. Also note that this child toy language can run on embedded systems, and doing memory safe IO concurrency is so easy it's hard not to, like oops I did ten thousand concurrent request, but it finished in less then a second, because this is 2016 and not 1986 and computing performance and memory has grown exponentially since. Don't get me wrong though, systems languages has their place in lower level where the bits, bytes and performance matters. Someone once told me that JavaScript is a lot like assembly because you are so free, there is no one telling you "No, you shouldn't do it that way" like in Rust.
- nixos 10y agoAnd in Java, and in any language with operator overloading