Hacker News new | past | comments | ask | show | jobs | submit login

TypeScript 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.



> 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/Tagged_union


At 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”


> 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.


That construct has been called a “record” since, I believe, 1970 in Pascal.


But then what name would they use for tuples, which are distinct from records?


both 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.


C# 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.


> 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-ref...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: