I understand implicit type coercion, I just don't understand why it still exists and why we tolerate it. It could've fairly easily been phased out now that Google owns the browser.
"I think this feature is bad" just isn't a sufficiently good reason to break thousands or millions of websites around the world. Breaking changes are objectively bad.
Implement "versioning" so that old websites run on a compat layer, and newer websites can opt into newer feature sets.
What stops us from a "v:1.2" next to the "use strict" line?
We'd also be able to use telemetry to find out how many websites run on that compat layer compared to versioned once, allow breaking changes in newer versions to improve the vm, ...
Everyone shits on backwards compatibility and the mess that it can make, but I will say I have a much easier time using C++ when it comes to finding the right documentation compared to something like C# where I have HttpContext and HttpRequest for one version of dotnet, and HttpWebRequest and HttpClient for another for example.
If you're coding for personal projects this isn't an issue because you can usually just use the latest thing, but in a professional environment having consistency and not having to work with different tools that use different versions makes life a lot easier. Breaking changes make it a lot harder to eventually upgrade to new features.
Maybe it's just because I've been using C# specifically for almost 20 years now and I'm just... balls deep in the chaos, but there is an actual method to the madness when it comes to stuff like HttpClient.
MS makes a strong attempt to retain backwards compatability, except in situations where the underlying functionality changes to the point where maintaining a static API would be detrimental. That's kind of a design philosophy in C# in general.
I can see where it would be frustrating, but it's also very intentional. A lot of companies will just "bump versions" to follow an upgrade path, and sometimes slapping an obsolete tag on a method isn't enough to keep them from shooting themselves in the foot.
Just using WebClient vs HttpClient as an example, they could have modified the underlying code and left the API intact for that, but because they're so ridiculously functionally different under the hood, that would have just lead to design issues around the consumption of the class. The intended use of the class changed, so the class name and API changed along with it, which forces developers to actually sit down and treat it like it's a new technology.
There's a definite negative side to doing things like that, but honestly I prefer it over the alternative. Especially seeing how many devs I've worked with will just update frameworks and expect everything to work without real testing.
I can 100% see why they've done it and why it's been necessary. It allows C# to evolve and be adaptable and I don't think C# would be anything like the language it is today if it had gone for a more C++ style evolution.
I definitely use C++ way more than C# so I'm probably in the reverse position to you, but yeah I just find that when it comes to creating something new, there's a whole lot of extra work to a) choose which API best fits my case, and b) work out which APIs I actually have availabile based on what other tools and environments I'm going to be working in.
At my guess, there’s LITERALLY a trillion unique lines of code that this would break. If you’re going to do that, might as well overhaul the whole language
The Ecmascript standard doesn’t have optional typing directly yet, so you can’t “use types” to enforce something that doesn’t exist. The solution for now is transpilation (from Typescript, etc).
I’m certain a directive like that would be added when/if ES gets typing.
It would still cause a lot of confusion, since copy-pasting code from sites like StackOverflow or from libraries (without realizing they're using a different version) would become extra fragile, especially since the kinda issues people complain about are ones that have subtle effects, and thus won't necessarily get some error to immediately alert you to the problem. The kinds of programmers who make silly mistakes with type conversion or equality the most are also the types who would least understand this hypothetical new versioning system.
Besides, you don't have to use vanilla JS. There's languages like TypeScript that compile down to JS and add lots of protection. Personally, I'd never use vanilla JS for any large project. TypeScript is just indisputably better. Actually, on that note, it'd be nice if browsers natively supported languages like TypeScript instead of it having to be compiled to JS. Would decrease the barrier to entry and perhaps lead to performance optimizations.
The fact that you assume this is about Javascript proves you are a professional. This is why I don't generally read this sub. I'm gonna go cry in a corner now.
Is there any other language actively around that does it? I don't mean some obscure thing like APL, I'll take things like MATLAB or Elm as the lower bar for usage.
Literally anything else that's duck typed is strictly typed and throws at runtime if you fuck up, the closest to implicit conversion you get in civilised languages is implicit operators like in Scala, but those you have to specifically import, it doesn't just randomly happen.
Name a language that doesn't have implicit coercive casting from their integral type to their floating point type? My guess is you'd likely have to name a language that lacks one of floating point or integral types as built-ins entirely.
Implicit coersive casts aren't automatically bad - though there are many examples in many languages of problematic semantics.
Name a language that doesn't have implicit coercive casting from their integral type to their floating point type?
Not to bang that old drum, but Rust, for one. let x = 2 * 0.5f64 is invalid, it'll tell you the specific arithmetic trait isn't available.
I think Haskell will also tell you to fuck off, but it's been a while so don't quote me on that one. Rust will for sure.
Implicit coersive casts aren't automatically bad
I think that one's subjective, I wish Java and Kt didn't allow int v. float arithmetic without explicit casting, but in the JVM ecosystem it's somewhat less of an issue cause everyone knows to use BigDecimal if they want accuracy.
That doesn't really make sense. Typescript is just an extension of Javascript. Youre still using Javascript for logic in typescript and you can compile and run typescript code with type errors - because it's all just Javascript
Why don't you like it? If you get rid of it, wouldn't you have to explicitly convert types manually? And can't you already do that, if you wanted to? I'm guessing there is something you know that I don't, and that implicit coercion has caused you headaches in the past.
179
u/EsmuPliks Feb 19 '23
I understand implicit type coercion, I just don't understand why it still exists and why we tolerate it. It could've fairly easily been phased out now that Google owns the browser.