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?
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?
"sum type" is the term of art in Computer Science theory, but "union type" is also used.
See
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.
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.
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 errorCS1061: '(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 throwsRuntimeBinderException: 'System.ValueTuple<int,int>' does not contain a definition for 'u'
at runtime, revealing the underlying CLR type.
Not always true in C#. Tuples can have member names
https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
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.