[your code here]

jfondren julian.fondren at gmail.com
Mon Sep 27 19:07:16 UTC 2021


On Monday, 27 September 2021 at 18:21:24 UTC, Dukc wrote:
> I sometimes feel that D is a bit weak when it comes to 
> representing and handling dynamic tree-like data. However, I 
> goofed around a bit and found that the newly added 
> `std.sumtype` can get D quite far in that regard:

Nice. The tradition of sumtypes comes through ML (Meta-Language, 
not Machine Learning) and later FP langs, which always have 
tutorial topics like "test if a tree is a Braun tree" and "write 
an evaluator for an expression type"

```ocaml
# type expression =
       Const of float
     | Var of string
     | Sum of expression * expression    (* e1 + e2 *)
     | Diff of expression * expression   (* e1 - e2 *)
     | Prod of expression * expression   (* e1 * e2 *)
     | Quot of expression * expression   (* e1 / e2 *)
```

so it's natural that these would translate to D. The different 
style makes some definitions more verbose

```d
// from std.sumtype docs
enum Op : string
{
     Plus  = "+",
     Minus = "-",
     Times = "*",
     Div   = "/"
}
alias Expr = SumType!(
     double,
     string,
     Tuple!(Op, "op", This*, "lhs", This*, "rhs")
);
```

but they still work. It'd be weird if trivial examples were very 
difficult :)


More information about the Digitalmars-d mailing list