www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Printing type name in static assert

reply "Martin Cejp" <minexew gmail.com> writes:
I'm trying to do something to this effect:

template XYZ(Class) {
...
static assert(index != -1, "No such annotation on " ~ 
typeid(Class));
...
}

However, typeid is not a string and I can't do to!string. Nor is 
typeid().name implemented at compile time.
What would be the correct way to do this?
Jan 25 2014
next sibling parent "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Saturday, 25 January 2014 at 11:28:40 UTC, Martin Cejp wrote:
 I'm trying to do something to this effect:

 template XYZ(Class) {
 ...
 static assert(index != -1, "No such annotation on " ~ 
 typeid(Class));
 ...
 }

 However, typeid is not a string and I can't do to!string. Nor 
 is typeid().name implemented at compile time.
 What would be the correct way to do this?

Class.stringof perhaps?
Jan 25 2014
prev sibling next sibling parent "Jason White" <54f9byee3t32 gmail.com> writes:
I think what you are looking for is Class.stringof:

     template XYZ(Class)
     {
         // ...
         static assert(index != -1,
             "No such annotation on "~ Class.stringof)
             );
         // ...
     }
Jan 25 2014
prev sibling next sibling parent "Jason White" <54f9byee3t32 gmail.com> writes:
On Saturday, 25 January 2014 at 11:41:54 UTC, Rikki Cattermole
wrote:
 ...

Damn, beaten by 1 second!
Jan 25 2014
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
On Saturday, 25 January 2014 at 11:28:40 UTC, Martin Cejp wrote:
 I'm trying to do something to this effect:

 template XYZ(Class) {
 ...
 static assert(index != -1, "No such annotation on " ~ 
 typeid(Class));
 ...
 }

 However, typeid is not a string and I can't do to!string. Nor 
 is typeid().name implemented at compile time.
 What would be the correct way to do this?

for just printing `Class.stringof` is most simple thing to do. There are also `__traits(identifier, Class)` and `std.traits.fullyQualifiedName!Class` that are more specialized and usually used in code generation.
Jan 25 2014
prev sibling next sibling parent "Martin Cejp" <minexew gmail.com> writes:
On Saturday, 25 January 2014 at 11:44:04 UTC, Dicebot wrote:
 On Saturday, 25 January 2014 at 11:28:40 UTC, Martin Cejp wrote:
 I'm trying to do something to this effect:

 template XYZ(Class) {
 ...
 static assert(index != -1, "No such annotation on " ~ 
 typeid(Class));
 ...
 }

 However, typeid is not a string and I can't do to!string. Nor 
 is typeid().name implemented at compile time.
 What would be the correct way to do this?

for just printing `Class.stringof` is most simple thing to do. There are also `__traits(identifier, Class)` and `std.traits.fullyQualifiedName!Class` that are more specialized and usually used in code generation.

Thanks, that will do. fullyQualifiedName was the first that came to mind, but it's broken.
Jan 25 2014
prev sibling parent "Martin Cejp" <minexew gmail.com> writes:
alias Field = typeof(__traits(getMember, Vertex, fieldName));

const bool normalized = HasAnnotation!(Vertex, fieldName, 
Normalized);
const int fieldOffset = __traits(getMember, Vertex.init, 
fieldName).offsetof;

int i = GetAnnotationInstance!(Vertex, fieldName, Position).pos;

glVertexAttribPointer(i, cast(GLint) numElements!Field, 
TypeToGLEnum!(ElementType!Field),
         normalized ? GL_TRUE : GL_FALSE, Vertex.sizeof, 
cast(void*) fieldOffset);
glEnableVertexAttribArray(i);


I'm starting to like this language :-)
Jan 25 2014