vvvv / VL-Language

The official repo for the design of the VL programming language
31 stars 0 forks source link

[Proposal] Sum Types / Tagged Union #22

Open gregsn opened 4 years ago

gregsn commented 4 years ago

Many functional languages come with a neat data type idea often called sum types.

https://en.wikipedia.org/wiki/Tagged_union

type Tree =
  | Leaf
  | Node of value: int * left: Tree * right: Tree

This sounds like a bit like an enum #12 on steroids: For each possible entry, you already have the possibility to attach some more data that makes sense for this entry only.

But not only that. You could also think of it as a built-in data type for implementing the state pattern #10 in a less verbose way. We'd be able to address #8 without the need to tell everybody about an abstract private data type and a bunch of private implementations of it.

We could maybe offer a data type (in that drop-down)

called Variant (or whatever).

And below that add a new section to add entries to that Variant.

You would now be able to

The system compiles to the state pattern (#10)

the interface additionally would surface

TBD in detail...

The main question here: Would that long term wish influence how we address #12? Or should this proposal be seen as an enhancement to an enum definition, somehow suggesting we should discuss this before defining how an enum definition should look like?

beyon commented 4 years ago

👍 for Sum types

Ideally VL would have them compatible with the rest of the .Net ecosystem, however I don't know how realistic it is to target F# compatibility.

C# might get them - but not before C# 10 at least (so you have to wait or be incompatible..) and it's not likely to be directly compatible with F#

woeishi commented 4 years ago

nice! missed that one. while it definitely makes sense to align with roslyn/.net implementation. probably worth peeking at rust-lang which implemented it deeply into its core.

without having read through the whole c# thread about it, i wonder how well a tagged union would integrate into the 'classic' enum and flags which heavily borrow functionality from their underlying (fixed size) value type, casting, numeric comparison, bit ops,...