Multiple alias this failed workaround...obscure error message
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 11 13:53:11 PDT 2014
> Sent: Wednesday, June 11, 2014 at 8:07 PM
> From: "matovitch via Digitalmars-d-learn" <digitalmars-d-learn at puremagic.com>
> To: digitalmars-d-learn at puremagic.com
> Subject: Multiple alias this failed workaround...obscure error message
>
> I was looking for a workaround to multiple alias this (or
> opImplicitCast) the following trick doesn't work (why shouldn't
> it ?). The error message is quite obscure to me.
>
>
> import std.stdio;
>
> class A(Derived)
> {
> alias cast(ref Derived)(this).x this;
> }
>
> class B : A!B
> {
> float x;
> }
>
> class C : A!C
> {
> int x;
> }
>
> void main()
> {
> B b;
> b.x = 0.5;
>
> float f;
> f = b;
> }
>
>
> output :
>
> source/app.d(5): Error: basic type expected, not cast
> source/app.d(5): Error: no identifier for declarator int
> source/app.d(5): Error: semicolon expected to close alias
> declaration
> source/app.d(5): Error: Declaration expected, not 'cast'
> Error: DMD compile run failed with exit code 1
I don't believe that it's legal to use a cast in an alias declaration, and
that's certainly what the error seems to be indicating. Also, using ref in a
cast is definitely illegal regardless of where the cast is. ref is not part
of a type. The only places that you can use it are function parameters, return
types, and foreach variables. If you want to do anything like you seem to be
trying to do, you're going to have to alias a function which does the cast for
you rather than try and alias the variable itself.
I also don't see anything here that would work as any kind of multiple alias
this.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list