7 ms·
Union types are horrendous in practice because they're non-compositional. None|T is usually disjoint except when it isn't, and it's really easy to forget that c
by lmm 3y ago
Union types are horrendous in practice because they're non-compositional. None|T is usually disjoint except when it isn't, and it's really easy to forget that case and fail to test it properly.
Having used Scala extensively, None as its own type is very much a mistake; it's not a type that you ever want and it only serves to get in the way.
- nyanpasu64 3y agoWould union types be manageable if you only use them with concrete types rather than generic or inferred ones, or is it too easy to accidentally refactor a None into one of the cases (possibly hidden behind a typedef)?
- lmm 3y agoA feature you can't use with generic types is pretty useless, frankly. So "manageable" yes, but the cure is worse than the disease.
- nyanpasu64 3y agoI wouldn't say non-generic union types are useless; in fact the vast majority of sum or union types I write in application code (not libraries) are a combination of two or more concrete types, rather than generic ones.
- tomjakubowski 3y agoWhat does it mean for None|T to be disjoint?
- lmm 3y ago"If it's None then it's not T". It's very natural to write code that handles the case where the value is None, handles the case where the value is T, but subtly malfunctions if T can be None (e.g. you might write a cache and use None to represent the value not being present in the cache - but then your cache silently fails to cache if the thing you were caching returns None).
- squirtlebonflow 3y agoHow is it natural to write code like that? If it's None, don't put it in the cache. If T is None, don't put it in the cache. if (typeof input != None) putInCache(input)
- lmm 3y ago> If it's None, don't put it in the cache. If T is None, don't put it in the cache. > if (typeof input != None) putInCache(input) Exactly, now you've just written exactly the bug I was talking about.
- nyanpasu64 3y agoIf T is int|None, then None|T can't distinguish between None and T(None), whereas None|Some(T) can distinguish between None and Some(T(None)).
- Gunax 3y agoIt seems like the issue then is that int can be nullable. The solution is to make int an int. You cannot assign None to int. I'm not an expert on programming languages, but asaik this is how typescript operates
- codebje 3y agoThat's not quite what the parent is trying to say. Assume that int is not None. Create a type called T that is int|None - a term of type T can be an int, or it can be None. Now have a function whose argument is T|None. Substitute in the definition of T and you get (int|None)|None. If the function's argument is None, what does that mean? Was it given a T that happened to be None, or was it not given a T? Nobody knows.
- scubbo 3y ago> Was it given a T that happened to be None, or was it not given a T? Hmm. It _feels_ like a function shouldn't ever need to differentiate between "what the caller thinks it has" (that is - being passed a "T-which-is-None" should have the same significance as being passed a "None"), but I'm not confident claiming that would ever be the case.
- codebje 3y agoConsider an expensive computation that may have no answer, and so it returns (after some heavy processing) "int|None". Also consider a cache lookup function that takes some key and returns a generic T|None for T the type of cache entries, with None signifying the key was not found. Both are, on their own, pretty reasonable things to have. If you are using union types and you put the results of your expensive computation into your cache, you will not be able to tell when you've done an expensive computation for some key and got a None result, or when you haven't got a result in the cache for that key.
- chongli 3y agoWait so in Rust you can’t have an Option<Option<T>>? That’s really bad!
- nyanpasu64 3y agoIn Rust you can, in Python an Optional[Optional[int]] is the same as an Optional[int].
- gemstones 3y agoYou can have this, I’ve used it in Rust before.
- rtpg 3y agoYou can, because Rust doesn’t support first class Union, and uses ADTs. Something like Typescript allows you to write “(T | null) | null)” but that is not the same thing as a double-Option’d type.
- ratmice 3y agoRust actually does have an untagged union type, but it generally requires some unsafe to use it, doesn't seem to get much use beyond c/ffi.
- agrover 3y agoIt was added specifically for c/ffi, so this is cool.
- rtpg 3y agobut the untagged union is still an aggregate at the type level right? Your data is a union at runtime, but at the type level it's a distinct thing, yeah? TS in particular is offering bespoke anonymous unions, which is a different sort of beast.
- ratmice 3y agoOf course, rust being nominally typed it is a distinct thing, while TS is structural. But that aspect should affect much more than just unions, (that said I personally know very little about typescript so talk my opinions here with a grain of salt).
- hn_throwaway_99 3y agoNot sure I follow your argument, but I use union types extensively in TypeScript and I don't think they're horrendous at all. E.g. it's very common to define a helper type like type Maybe<T> = null | undefined | T and then with TypeScript's flow analysis I can easily determine when it's safe to call properties on a value, e.g. const foo: Maybe<Foo> = ... if (foo) { // Typescript now guarantees foo is not null or undefined // thus it's a Foo, so I can call any Foo methods on it foo.bar(); }
- lmm 3y ago> Not sure I follow your argument, but I use union types extensively in TypeScript and I don't think they're horrendous at all. Have you ever used a language with a good implementation of sum types? > and then with TypeScript's flow analysis I can easily determine when it's safe to call properties on a value The problem isn't calling methods on null or undefined. The problem is when the value is null or undefined, but not for the reason you think (e.g. you think your code set it to null, but actually the generic code you called came back with null: T), and so you mix up the semantics.
- auggierose 3y agoNot the parent, but I have used Standard ML, OCaml, Scala and Swift, and I like union types as well. They are strictly more powerful than sum types. If I want to create an Option type such that Option<Option<T>> = Option<T>, then that's possible. With sum types only, it is not. Yes, this gives you Option<T> = T in situations where T = Option<S>. If I want to have a sum type, then I can just create a distinguishing "kind" field, as you do in TypeScript. The argument against union types would be, that they are an advanced feature compared to sum types, and sum types are simpler to use right. The argument against sum types is that they are moving the role of types from descriptive to prescriptive, and that is what leads to the problem Hickey is pointing out.
- rtpg 3y agoWhile I get what you're referring to, the issue of Typescript getting this mixed up is not a real issue. Why? If you write a function that takes T | null and returns T, then you're going to need to filter out your null. Now, Typescript isn't safe, so you can have f: (T | null) -> T and give it a (null | null) and its return will indeed be null (at least at the moment, see [0]). But your function's type guards will, at one point, filter out the null case. You might try doing this through some assertion, but if you do the runtime check, it will in fact blow up at runtime. So Typescript will allow invalid code, but if you are actually doing type assertions correctly you'll get a runtime error at the "type narrowing" step. Worse than static verification of course, but better than carrying around a null that you think is not null. Anyways, yeah, the conclusion is that in TS at least, T | null narrowing to T doesn't mean that T is not null. TS is at least smart enough to handle that. [0]: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABGOA5EAbDAeAKgPgAoAPALkV0QB9lMMBKcygbwChEPEZhETEBeQbSz1EbThMRQAFgCc4Ad2QBTJQFFZ82YQBEYOgEId9dpwC+pjrOVQQspMVYXWUAJ4AHZYgDy72Ajx8AQpqYQxWVggEAGcoRABbAENXACNlXFkYCABrV3JffzBsfSwg-jCAbgiosFipADdyEoxglHQsQiTU9Myc13oqyJi4DGUAOgw4AHNdAFlXRGUwABNEesSMEC8YaMQdABoGk1YgA https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABGO...