6 ms·
Statically typed languages without Sum Types (aka tagged unions aka enums) which includes Java, C# and C++ amongst others drive me crazy: they have no ergonomic
by nicoburns 6y ago
Statically typed languages without Sum Types (aka tagged unions aka enums) which includes Java, C# and C++ amongst others drive me crazy: they have no ergonomic way to express "or" types. This is a massive expressive hole which (along with lacking type inference) I believe is responsible for a lot of the hate towards statically typed languages.
- lolinder 6y agoSealed classes in Kotlin largely solve that problem for me, and Java is getting those. It's not quite the same, but most of the time I find that if I'm trying to do Int|String, it's primitive obsession and there is actually a better sealed type hierarchy I'm missing.
- valenterry 6y agoYes, totally agree. It's the lack of statical type systems like this one that makes people hate them - for good reasons.
- NOGDP 6y agoThese languages have far better type systems than languages like python or javascript, that's not a reason to hate them.
- valenterry 6y agoWe are talking about user experience here. In that regard, comparing python/javascript with Java/C++ is comparing apples with oranges.
- NOGDP 6y agoWe are talking about Python and JavaScript from the top of this comment chain, and the 'user experience' of writing Java/C# is closer to Python and JavaScript than to, say, Haskell.
- nicoburns 6y agoYes, but I find that the user experience of writing Rust or Swift is closer to that of JS than C# and Java.
- NOGDP 6y agoWhy? Rust's type system is basically a more sophisticated version of Java's, JS is in the opposite direction - a much simpler dynamic type system. Rust's lifetimes and borrow checker is additional complexity that JS doesn't have. Rust has longer compilation times than JS, longer than Java. Etc.
- valenterry 6y agoFirst of all, of course Rust hast some additional complexity because it is close to bare metal. But if you think this complexity away (to make it comparable to e.g. javascript), here are some reasons: 1) Better type-inference. In Java this has improved but is still much more clunky and boilerplatey. Good type-inference is important to not annoy the user. 2) Traits / type-classes. They enable a way of programming that comes much closer to duck-typing and avoid wrapping your objects in wrapper-classes to support interfaces like you are forced to do it in Java. 3) Better and less noisy error handling (looking at you Java, Go, C++ and most other languages)
- NOGDP 6y agoYou can't 'think away' the additional complexity of lifetimes and the borrow checker though. It's something you have to understand and keep in mind. > Better type-inference. In Java this has improved but is still much more clunky and boilerplatey. Good type-inference is important to not annoy the user. In my opinion, Java without type inference is fine - it's very minor issue, and there is a fairly limited scope of code that would actually benefit from type inference in terms of quality/readability. If you use a decent editor most of the redundant typing is auto-completed anyway. > Traits / type-classes. They enable a way of programming that comes much closer to duck-typing and avoid wrapping your objects in wrapper-classes to support interfaces like you are forced to do it in Java. Eh, Rust traits are better than Java's interfaces, but you can implement multiple interfaces for your own objects in Java without any wrappers. The issue is extending external objects to support new interfaces. Plus, the point is to have correct code defined and checked at interface/trait boundaries, something JS doesn't do at all. > Better and less noisy error handling (looking at you Java, Go, C++ and most other languages) The error messages for Rust can be much more complex than Java, and are probably more complex on average, simply because it's a more complex language and type system. I would say the benefits of Java's type system far outweigh the imperfections and tiny costs when compared to a language like JS.
- NOGDP 6y agoModern versions of these languages have these features to a varying degree.
- bigyikes 6y agoThis is one of the joys of TypeScript. Some of my coworkers complain about being required to use TS instead of JS, and I just wonder why in the world you would want to use JS in a massive codebase.
- IshKebab 6y agoIt is, although the syntax for proper sum types (it calls them "discriminated unions") is really verbose in Typescript. I wish they'd make it more terse so people didn't use untagged sum types and hacks like `typeof` all the time.
- jmfldn 6y ago> Statically typed languages without Sum Types (aka tagged unions aka enums) which includes Java, C# and C++ amongst others drive me crazy: they have no ergonomic way to express "or" types. This is a massive expressive hole which (along with lacking type inference) I believe is responsible for a lot of the hate towards statically typed languages. Union types coming in Scala 3
- junon 6y agostd::variant or std::any don't count? Overloading doesn't count? Templates don't count? Seems like you should play with C++ a bit more.
- valenterry 6y agoNot a C++ developer, but from what I read, it says: "A variant is not permitted to hold references, arrays, or the type void". These are quite some limitations and don't really give a "smooth" experience.
- junon 6y agoNone of those types make sense in variants. This is obvious to C++ programmers. With all due respect, if you're not qualified to make assertions about something, perhaps refrain from labeling it as "quite the limitation".
- valenterry 6y agoCan you explain why it makes no sense for it to hold an array or void? I'm really curious and will take back my claim.
- junon 6y agoYou cannot trivially compare or move an array. Note that `std::array` is still allowed in `std::variant` - just not C-style arrays. As for references, you cannot re-bind references. Rationale here: https://stackoverflow.com/a/27037871/510036 https://stackoverflow.com/a/27037871/510036 As for void, apparently the reasons I had in my head are not the reasons in real life. I thought it might be because of a destructible requirement on the type, but it turns out there really isn't a good reason why they disallowed it, and that a future standard might allow it. In any event, there are a multitude of variant implementations that allow all sorts of things depending on the behavior you want. Nothing is forcing you to use the standard library.
- 6y ago
- flohofwoe 6y agoIf you care about the exact underlying memory layout such high-level types are way too blackbox-y. I doubt that historically this specific feature was responsible for any "static typing hate" (because languages with such high-level type systems were quite obscure 10 or 20 years ago). I have the impression that this hate was specifically a web-dev thing because many Javascript developers never experienced what it's like to work with a statically typed language until Dart and Typescript showed up (and then it suddenly was the best thing since sliced bread).