Hacker News new | ask | show | jobs
by FjordWarden 354 days ago
> we turn our attention to the dual of records – variants.

How is this the case, can someone give me an example?

3 comments

Records are a logical and. A cat is a name and an age and a color, for example.

A variant is a logical or. A pet is a dog or a cat.

And and or are duals.

Much more in the "Algebraic data types" chapter at https://scalawithcats.com/

Does that answer it?

Ok, sum types and product types
Yes.
For immutable records and variants, if you have a set of labels A that is a subset of label set B (or corresponding label–type maps), then a record of A is a supertype of a record of B, while a variant of A is a subtype of a variant of B.

For example: A record type {x:X, y:Y} is a supertype of {x:X, y:Y, z:Z} (all values of the second can stand in as values of the first), while a variant type x:X | y:Y is a subtype of x:X | y:Y | z:Z (all values of the first can stand in as values of the second).

The sibling comments refer to AND and OR (which isn't accurate, as variants are really XOR), or to product and sum types, but that doesn’t demonstrate the actual duality.

Records are product types. Variants are sum types.

https://www.cs.princeton.edu/courses/archive/fall05/cos441/n...