Custom Type Unions
Dhruv Singal
dsingal at calpoly.edu
Sun May 26 17:59:21 UTC 2019
I'm trying to implement an interpreter in D as a class project.
Having already written the the parser and interpreter in Racket,
I'm having trouble figuring out if dlang has a way for me to
create a union object that can be one of many object and have it
be a first-class object, ie it can be the return type of
functions. Is this something dlang supports? If not, is there a
better way of implementing this?
Below is some rudimentary code I wrote to test this:
import std.stdio;
import std.string;
alias string = immutable(char)[];
union Value {real realV; bool booV; string stringV; primV prim;};
struct primV{
real function(int, int) doer;
}
real add (int a, int b){
real sum = a+b;
return cast(real)sum;
}
void main(){
writeln("Beginning Interp");
primV adder = primV(&add);
real x = adder.doer(5,6);
writeln(x);
}
More information about the Digitalmars-d
mailing list