ozra / onyx-lang

The Onyx Programming Language
Other
97 stars 5 forks source link

Request: Tagged unions #104

Closed Sod-Almighty closed 7 years ago

Sod-Almighty commented 7 years ago

Tagged unions - also called discriminated unions, disjoint unions, sum types or case classes - are very useful for writing terse code. We should have them.

In addition, they are generally consumed by a handy match construct, similar to a switch.

ozra commented 7 years ago

Current Onyx:

foo(x Foo|Bar|Qwo) ->
   match x
      Foo => say "a Foo: { x.foo-stuff }"
      Bar => say "a Bar: { x.bar-stuff }"
      Qwo => say "a Qwo: { x.qwo-stuff }"
      *   => say "This won't happen"

a = switch rnd 1..3
   1 => Foo()
   2 => Bar()
   3 => Qwo()

foo a

(match is currently just an alias for switch — still awaiting settling of some other features before implementation with destructuring syntax)

It should be rather clear at this point that union types are a fundamental part of Onyx?

Sod-Almighty commented 7 years ago

No, to really use tagged unions, you need to be able to do destructuring like this:

match object
  Circle x y r => say "A circle with radius {r}"
  Rect x1 y1 x2 y2 => say "A rectangle from {x1}, {y1} to {x2,y2}"
ozra commented 7 years ago

Was that tl;dr for you? ;-)

still awaiting settling of some other features before implementation with destructuring syntax