Compile-time evaluation lost in alias this

kenji hara k.hara.pg at gmail.com
Wed May 30 08:04:28 PDT 2012


2012/5/30 Tommi <tommitissari at hotmail.com>:
> struct ValueImpl
> {
>    static immutable(int) getValue()
>    {
>        return 42;
>    }
> }
>
> struct ValueUser
> {
>    ValueImpl m_valueImpl;
>    alias m_valueImpl this;
> }
>
> void main(string[] args)
> {
>    static assert(ValueImpl.getValue() == 42);
>    static assert(ValueUser.getValue() == 42); // (1)
> }
>
> (1) Doesn't compile. Error: variable m_valueImpl cannot be read at compile
> time
>
> To use alias this for composition, it would be nice if the compile-time
> evaluable members of the aliased object would remain compile-time evaluable
> even when used through an alias. Is the current behavior defined by the
> language or is it due to the compiler (DMD) implementation?

Looks like a bug.

ValueUser.getValue() is translated to ValueUser.m_valueImpl.getValue()
with alias this resolution. In here:
1. ValueUser.m_valueImpl is a valid expression. It means just a symbol
of variable which declared in ValueUser struct.
2. ValueUser.m_valueImpl is only used for overload resolution of
calling getValue() and getValue is static member function, then it is
*never* evaluated even in run-time.

Please file a report about it in bugzilla.

Kenji Hara


More information about the Digitalmars-d mailing list