CTFE UFCs?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 14 09:52:19 PDT 2015


On Tuesday, 14 April 2015 at 15:20:37 UTC, bitwise wrote:
> When I uncomment the nicer syntax, I get the errors below:
>
> [1] Error: variable refl cannot be read at compile time
> [2] Error: CTFE failed because of previous errors in base
>
>
> class Base {
>     double d = 0.4;
> }
>
> class Test : Base {
>     int x = 4;
> }
>
> abstract class Refl {
>     @property abstract string name() const;
>     @property abstract string baseName() const;
> }
>
> class ClassRefl(T) : Refl {
>     @property override string name() const {
>         return T.stringof;
>     }
>
>     @property override string baseName() const {
>         alias BaseClassesTuple!T base_types;
>         return base_types[0].stringof;
>     }
> }
>
> const(Refl) reflect(T)() {
>     static const(Refl) refl = new ClassRefl!T;
>     return refl;
> }
>
> const(Refl) reflect(string name)() {
>     mixin("return reflect!(" ~ name ~ ");");
> }
>
> //const(Refl) base(const(Refl) refl) {
> //    return reflect!(refl.baseName());        ERROR  [1]
> //}
>
> void main()
> {
>     static const(Refl) refl = reflect!Test;
>
>     static const(Refl) baseRefl = reflect!(refl.baseName());
>
>     //static const(Refl) baseRefl = refl.base;       ERROR  [2]
>
>     writeln(refl.name);
>     writeln(baseRefl.name);
> }
>
>
> If anyone can offer a work around it would be much appreciated.

Minimal changes to what you have now; gives you `base!refl` 
instead of `refl.base`:
----
const(Refl) base(alias refl)() {
    return reflect!(refl.baseName());
}
static const(Refl) baseRefl = base!refl;
----

Digging deeper:
----
abstract class Refl {
     @property abstract string name() const;
     immutable(Refl) base() const;
}

class ClassRefl(T) : Refl {
     @property override string name() const {
         return T.stringof;
     }

     override immutable(Refl) base() const
     {
         alias BaseClassesTuple!T base_types;
         static if(base_types.length > 0)
         {
             static immutable(Refl) instance = new 
ClassRefl!(base_types[0]);
             return instance;
         }
         else return null;
     }
}
static const(Refl) baseRefl = refl.base;
----

Parting thoughts:
I don't know where you're heading with this. But so far I don't 
see what it would buy you over std.traits and 
TypeInfo/TypeInfo_Class.


More information about the Digitalmars-d-learn mailing list