struct subtyping?
spir
denis.spir at gmail.com
Sun Oct 24 16:30:34 PDT 2010
On Sun, 24 Oct 2010 23:41:07 +0200
"Simen kjaeraas" <simen.kjaras at gmail.com> wrote:
> spir <denis.spir at gmail.com> wrote:
>
> > I would like to learn about possible workarounds, or other alternative
> > approaches to such problems, if ever you now any.
>
> Basically, you probably want to use alias this:
>
> http://digitalmars.com/d/2.0/class.html#AliasThis
>
> It lets you subtype structs by propagating member field/function access
> to a member, if the struct or class does not implement the field or
> function itself.
Seems I first misunderstood: alias this lets one alias _one_ field as fake this? I experimented with this:
struct NamedConstant
{ float value ;
string name ;
alias value this ;
string toString () { return this.name ;}
}
void main () {
NamedConstant PI = NamedConstant(3.14,"pi") ;
writefln("%s * 2 = %s", PI, 2*PI) ;
}
This is similar to the ability offered by some languages to subtype builtin types, no? The slot on which lookup is delegated should actually be an instance of root type? (I had thought exactly the opposite.)
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
More information about the Digitalmars-d-learn
mailing list