5 ms·
> In a dynamically typed language, this would be a + b. In a statically typed language, once you consider overflow and underflow, you will likely have a hundred
by paulmd 3y ago
> In a dynamically typed language, this would be a + b. In a statically typed language, once you consider overflow and underflow, you will likely have a hundred lines of code.
https://stackoverflow.com/questions/52376716/c-overloading-of-the-plus-operator https://stackoverflow.com/questions/52376716/c-overloading-o...
literally a single line of code to overload an operator.
anyway, if you don't consider error states in your dynamic implementation and you do this in static then yes, it could be simpler. But static doesn't force you to consider error conditions - just like this C++ example does not.
and yes, if you fall back on javascript notions of default cross-type arithmetic (1 + "1" = ?) then you will get something, and if you take great care to structure your code you may get something useful as a side-effect, but, this is not meaningfully different from the error-state for most people. Getting [] or "11" isn't the expected outcome unless you've come to expect that quirk of javascript, and is something that a static compiler would rightfully complain about - because a user asking for an undefined operation computed across incompatible types seems like a classic type error. If it's not, then just define the operators that are meaningful to you - and you could define it as an interface if you wanted it to work with a bunch of classes etc.
Again though, the complaint elsewhere that "a lot of this debate just ends up being dynamic programmers who are unaccustomed to using static types at all and think it must be so super burdensome all the time" seems pretty accurate. Defining custom operators is not something that occupies even 1% of my time in any static language.
- ReflectedImage 3y agoThe code you posted is incorrect and full of bugs. For example, if A is 9223372036854775807 and B is 10, then you will get the incorrect answer. Try it out if you don't believe me. Another example is A is -9223372036854775807 and B is -10. Again you will get an incorrect result. Once you finish fixing these bugs it will be at least 100 lines long. The dynamically typed version of the code is just A + B. "a lot of this debate just ends up being dynamic programmers who are unaccustomed to using static types at all" Static typing is objectively the inferior approach. It's just that people are too lazy to learn how to code with two different typing systems. Overall statically typed programmers only code at 1/3 of the speed as their dynamically typed counterparts. Of course, if you have only ever done statically typed programming, you are not going to understand how bad it is in comparsion.
- therealdrag0 3y agoThat doesn’t sound like it’s typings fault at all. That’s a language design choice. Typed languages have types that handle adding numbers of all sizes same as dynamic typing. In fact JavaScript numbers aren’t magic, they are 64bit doubles, which have their own strange behavior and special cases, many of which could be considered “incorrect”. You’re acting like dynamic typing solves problems that it doesn’t solve.
- ReflectedImage 3y agoYou have errors coming from using static types. Static typing blew up a space rocket, if you weren't aware. JavaScript is terrible because it was built over 3 decades by different competing browser vendors.
- ReflectedImage 3y agoI think we are at max reply depth but here is the trivial dynamically typed solution to the problem: https://pastebin.com/UZvB5YD1 https://pastebin.com/UZvB5YD1 It's a little bit simpler than anything you will get in an statically typed language. It works for integer and floats of any size. It even works for lists! You can't do that in a statically typed language without a ton of code.
- fennecfoxy 3y agohttps://pastebin.com/gcsNs6Sw https://pastebin.com/gcsNs6Sw Oh, look. Something that statically typed languages solves.
- ReflectedImage 3y agoThat problem is also solved by dynamically typed languages. Your IDE picks it up.
- fennecfoxy 3y agoTrue, but if I know devs and management's "last minute and underplanned" culture IDEs can be ignored.