taggedalgebraic 0.11.0 adds TaggedUnion

Jacob Carlborg doob at me.com
Sat Feb 23 08:51:18 UTC 2019


On 2019-02-22 18:09, Sönke Ludwig wrote:
> TaggedUnion is the low level tagged union functionality separated out 
> from TaggedAlgebraic. Because it doesn't support transparent access of 
> methods and operators of the contained value, it is able to provide a 
> number of convenience features. On top of that, visit and tryVisit is 
> now supported for pattern matching, analogous to the version for 
> std.variant.Algebraic. This is the basic usage:
> 
>      union U {
>          string caption;
>          int height;
>          int width;
>      }
>      alias Value = TaggedUnion!U;
> 
>      auto val = Value.caption("foo");
>      assert(val.kind == Value.Kind.caption);
>      assert(val.value!(Value.Kind.caption) == "foo");
> 
>      // shorthand syntax:
>      assert(val.isCaption);
>      assert(val.captionValue == "foo");
> 
>      // set a different type/kind
>      val.setHeight(10);

Why not using property syntax, i.e. `val.height = 10`?

>      val.visit!(
>          (int i) { assert(i == 10); }
>          (string s) { assert(false); }
>      );

How does this handle the above case when there are two `int`? How do I 
know if it's the "width" or "height" that has been set?

-- 
/Jacob Carlborg


More information about the Digitalmars-d-announce mailing list