True value semantics for class or struct?
js.mdnq
js_adddot+mdng at gmail.com
Thu Nov 29 18:25:07 PST 2012
Simple values are ones that act atomically and use the standard
mathematical operations.
Suppose I would like to wrap an int with additional functionality:
struct SmartInt{
privatedata
int Value;
methods
}
The idea is that SmartInt semantics are treated identical to
SmartInt.Value
So SmartInt = 3; is identical to SmartInt.Value = 3;
But since it is a smart int we can do
SmartInt.SomeMethod()
Also, when a the struct is copied I might not want to copy
privatedata as it might change the functionality(maybe instead of
copying I want it to be set to 0, through a constructor, or
method).
The main purpose though, is to treat SmartInt and SmartInt as one
and the same semantically. As far as I'm concerned, SmartInt is
an int... I just "wrapped" it up with some nice functions.
Is anything like this possible in D?
More information about the Digitalmars-d
mailing list