#Union types and how to construct variants

1 messages · Page 1 of 1 (latest)

gentle kraken
#

If I have a union type, i.e.:

pub type node {
  v1(f: string, g: int)
  v2(j: float, k: int)
}

How does one construct and access the fields of v1 or v2? Is it as simple as let-binding one of the constructors, or is it not possible to construct such a type? The tutorial doesn't really answer this question.

dry nymph
#

let val = V1("wibble", 1)

#

to access val's properties, you need to match

case val {
  V1(f: f, g: g) -> ...
  V2(j: j, k: k) -> ...
}
gentle kraken
#

Could you just match on v1(name1, name2) or do you have to include annotations?

dry nymph
#

you could, sure

gentle kraken
#

Oh okay, thanks!

dry nymph
#

and you can match partially with V1(f, ..)