4 ms·
Is the terminology slightly off? AIUI TypeScript has type unions. But this looks like a discriminated union which I'd recognise from F# or Haskell. The distinc
by orra 2y ago
Is the terminology slightly off? AIUI TypeScript has type unions.
But this looks like a discriminated union which I'd recognise from F# or Haskell. The distinction I'd draw is that for a DU there are named case constructors. Yes?
- wk_end 2y agoTypeScript has "union types". This proposal refers to them as "ad hoc unions". I don't think "type union" is a term of the art - at least I haven't heard it before. It seems to be something the C# people are making up to describe sum types.
- SideburnsOfDoom 2y ago> I don't think "type union" is a term of the art "sum type" is the term of art in Computer Science theory, but "union type" is also used. See https://en.wikipedia.org/wiki/Type_theory#Sum_type https://en.wikipedia.org/wiki/Type_theory#Sum_type https://en.wikipedia.org/wiki/Tagged_union https://en.wikipedia.org/wiki/Tagged_union
- zarathustreal 2y agoAt this point, calling it by its correct name (Sum Types) would leave them looking stupid for not using the correct name (Product Types) for their “records”
- jcparkyn 2y agoBut then what name would they use for tuples, which are distinct from records?
- cies 2y agoboth tuples and record (structs) are product types. tuples are "untagged", records/structs are "tagged" (the members have names). sum types also have tagged and untagged variants. the untagged variants are less useful (more cumbersome) in case of sum types and hence often are not implemented in languages.
- SideburnsOfDoom 2y ago> tuples are "untagged", records/structs are "tagged" (the members have names). Not always true in C#. Tuples can have member names https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples#tuple-field-names https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
- jasomill 2y agoC# tuple field names are an interesting case, spiritually similar to this proposal's ad hoc unions: persisted in compiled output via attributes, respected by the compiler even across assembly boundaries, but not actually part of the resulting CLR type. So, e.g., if var p = (x: 1, y: 2); var q = (u: 1, y: 2); var f = ((int x, int y) a) => a.x + a.y; then p == q f(p) == 3 f(q) == 3 p.GetType() == q.GetType() are all true, but var r = q.x; yields the compile-time error CS1061: '(int u, int v)' does not contain a definition for 'x' and no accessible extension method 'x' accepting a first argument of type '(int u, int v)' could be found (are you missing a using directive or an assembly reference?) and if dynamic d = q; then var s = d.u; compiles, but throws RuntimeBinderException: 'System.ValueTuple<int,int>' does not contain a definition for 'u' at runtime, revealing the underlying CLR type.
- scott_s 2y agoThat construct has been called a “record” since, I believe, 1970 in Pascal.
- SideburnsOfDoom 2y ago> not using the correct name (Product Types) for their “records” There is no single "correct name" as there is no single "problem domain" that covers the algebra of type theory, and the pragmatics of programming in engineering, along with other areas.
- textlapse 2y agoGoing from C# to Typescript with type unions it always felt weird. Maybe that’s a good reason to have held off on this for this long? Know your audience and all that? It does get used to but still reading the too many A|B|undefined gets tiring after a while. It also adds to laziness as you could take one thing and be fuzzy about returning a combo of three or more things. And as you go up the stack this gets more and more confusing. C# forces you to deal with this in a constrained way which I like. But perhaps there are folks who are excited by this that have a convincing argument? Please educate me.