variant
Defines a Variant type.
You can use this type to implement variant/sum types. For example:
from utils.variant import Variantalias IntOrString = Variant[Int, String]fn to_string(inout x: IntOrString) -> String: if x.isa[String](): return x.get[String]()[] # x.isa[Int]() return str(x.get[Int]())[]# They have to be mutable for now, and implement CollectionElementvar an_int = IntOrString(4)var a_string = IntOrString(String("I'm a string!"))var who_knows = IntOrString(0)import randomif random.random_ui64(0, 1): who_knows.set[String]("I'm actually a string too!")print(to_string(an_int))print(to_string(a_string))print(to_string(who_knows))
Variant
A runtime-variant type.
Data for this type is stored internally. Currently, its size is the largest size of any of its variants plus a 16-bit discriminant.
You can - use isa[T]()
to check what type a variant is - use take[T]()
to take a value from the variant - use get[T]()
to get a value out of a variant - This currently does an extra copy/move until we have lifetimes - It also temporarily requires the value to be mutable - use set