Discriminated Unions

Philippe Sigaud philippe.sigaud at gmail.com
Wed Nov 20 10:18:44 PST 2013


On Tue, Nov 19, 2013 at 11:31 PM, Timon Gehr <timon.gehr at gmx.ch> wrote:

>
> In essence, the following is easily possible in theory:
>
> mixin ADT!q{
>  List(T):
>  | Nil
>  | Cons T List!T
> };
>
> auto list(R)(R r) if(isInputRange!R){
>     if(r.empty) return Nil!(ElementType!R);
>     auto f = r.front;
>     r.popFront();
>     return Cons(f,list(r));
> }
>
> size_t length(T)(List!T l){
>     return l.match!(
>         ()=>0,
>         (x,xs)=>1+length(xs)
>     );
> }

I concur, I did it also a few years ago. IIRC, I created an abstract
base class (List) and different subtypes. The associated matching
function was also generated at the same time.

What did you use to encode the types in your case? A tagged union?


More information about the Digitalmars-d mailing list