Translating Modula2 into D: variant records, pointer

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Jan 9 08:24:13 PST 2007


Jarrett Billingsley wrote:
> One thing I'm not real sure on is the variant record.  The closest thing I 
> think in D is a union, but nothing prevents you from accessing either tsym 
> or nSym in Node at any time.  I guess in Modula2, if 'terminal' is true, you 
> can only access tsym, and otherwise you can only access nSym? 

I think you can mostly fake this in D using private (differently-named) 
union members and property methods that assert if 'terminal' has the 
wrong value.
You can even make it so that the original names directly alias the 
private names in a non-debug build, and only use the checking version in 
a debug build[1].

Limitations:
* You can still access the private members (under the different name) 
from within the same module.
* You can't use operators with side-effects on the members (except for 
assignment of course). So no ++, +=, --, -=, ~=, etc.


[1]: Just make sure the code compiles in debug builds if you do this, 
because of the second limitation above (since it doesn't apply to the 
suggested non-debug version).


More information about the Digitalmars-d-learn mailing list