question about multiple alias this

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 24 10:46:57 PDT 2015


On Thursday, September 24, 2015 13:04:20 steven kladitis via Digitalmars-d-learn wrote:
> class A{
>    int i;
>    bool b;
>    alias i this;
>    alias b this;
> }
>
> void main()
> {
>    auto a = new A;
>    int i = a;
>    bool b = a;
> }
>
> --- this will not compile in dmd 2068.1.
> --- ok, but what technique do you use to emulate this type of
> behavior??
> --- would interfaces be the answer, if so could I see an example.
> and would you use the
> --- same for structs ??????
> --- thanks, Steven

The only implicit conversion that's built into user-defined types is casting
from a derived class to a base class or interface that it implements. All
other implicit conversions have to be implemented via alias this, and
currently, you can only have one alias this. So, what you're trying to do
really doesn't work right now. You could declare an opCast for each type you
want to be able to convert to and do explicit casts, but alias this is the
only way to add implicit conversions.

We'll probably have support for multiple alias thises at some point, but I
don't know when.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list