Re: Template mixin enum stringof
On Wed, 10 Dec 2014 14:32:12 + Lemonfiend via Digitalmars-d-learn wrote: > > mixin template S(alias fld) if (is(typeof(fld) == enum)) > > { > > import std.conv : to; > > enum s = to!string(fld); // "BAR" > > // or this: > > //import std.traits : fullyQualifiedName; > > //enum s = to!string(fullyQualifiedName!(fld)); // > > "test.Foo.BAR" > > } > > > > enum Foo { BAR, } > > > > void main() > > { > > mixin S!(Foo.BAR); > > } > > Perfect, thanks. p.s. be careful with imports, as they will go to the same scope as `s`. this may or may not be important. signature.asc Description: PGP signature
Re: Template mixin enum stringof
On Wednesday, 10 December 2014 at 14:25:30 UTC, ketmar via Digitalmars-d-learn wrote: On Wed, 10 Dec 2014 13:58:20 + Lemonfiend via Digitalmars-d-learn wrote: On Wednesday, 10 December 2014 at 12:57:16 UTC, ketmar via Digitalmars-d-learn wrote: > On Wed, 10 Dec 2014 12:35:44 + > Lemonfiend via Digitalmars-d-learn > > wrote: > >> On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via >> Digitalmars-d-learn wrote: >> > On Wed, 10 Dec 2014 11:52:11 + >> > Lemonfiend via Digitalmars-d-learn >> > >> > wrote: >> > >> >> Consider the following: >> >> >> >> --- >> >> enum Foo { BAR, } >> >> >> >> mixin template S(string s_) >> >> { >> >> enum s = s_; >> >> } >> >> >> >> void main() >> >> { >> >> mixin S!(Foo.BAR.stringof); // Error: identifier >> >> 'stringof' of 'Foo.BAR.stringof' is not defined >> >> >> >> enum e = Foo.BAR.stringof; >> >> mixin S!(e); // works fine >> >> } >> >> --- >> >> >> >> Why doesn't the first work? And is there an alternative >> >> to the second version? >> > >> > mixin S!((Foo.BAR).stringof); >> > >> > sorry, don't remember the parsing rule for this. >> >> Wow, I didn't even consider that.. Thanks! > also, you can use this: > > import std.conv : to; > ... > mixin S!(to!string(Foo.BAR)); > > people tend to forget that many `to!` variants are usable in > CTFE. Seems like I'd want to move the converting-to-string functionality to within the template mixin then, something like: --- mixin template S(T, T t) if (is(T == enum)) { //import std.traits; //enum s = fullyQualifiedName!(t); // unfortunately this results in "t" import std.conv: to; enum s = to!string(t); // this works } mixin S!(Foo, Foo.BAR); --- But passing an enum as parameter seems to be somewhat annoying. If I leave off the first Foo, then it complains about no-matching template for int parameter. !(Foo, Foo.BAR) seems kinda redundant.. something like this? mixin template S(alias fld) if (is(typeof(fld) == enum)) { import std.conv : to; enum s = to!string(fld); // "BAR" // or this: //import std.traits : fullyQualifiedName; //enum s = to!string(fullyQualifiedName!(fld)); // "test.Foo.BAR" } enum Foo { BAR, } void main() { mixin S!(Foo.BAR); } Perfect, thanks.
Re: Template mixin enum stringof
On Wed, 10 Dec 2014 13:58:20 + Lemonfiend via Digitalmars-d-learn wrote: > On Wednesday, 10 December 2014 at 12:57:16 UTC, ketmar via > Digitalmars-d-learn wrote: > > On Wed, 10 Dec 2014 12:35:44 + > > Lemonfiend via Digitalmars-d-learn > > > > wrote: > > > >> On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via > >> Digitalmars-d-learn wrote: > >> > On Wed, 10 Dec 2014 11:52:11 + > >> > Lemonfiend via Digitalmars-d-learn > >> > > >> > wrote: > >> > > >> >> Consider the following: > >> >> > >> >> --- > >> >> enum Foo { BAR, } > >> >> > >> >> mixin template S(string s_) > >> >> { > >> >> enum s = s_; > >> >> } > >> >> > >> >> void main() > >> >> { > >> >> mixin S!(Foo.BAR.stringof); // Error: identifier > >> >> 'stringof' of 'Foo.BAR.stringof' is not defined > >> >> > >> >> enum e = Foo.BAR.stringof; > >> >> mixin S!(e); // works fine > >> >> } > >> >> --- > >> >> > >> >> Why doesn't the first work? And is there an alternative to > >> >> the second version? > >> > > >> > mixin S!((Foo.BAR).stringof); > >> > > >> > sorry, don't remember the parsing rule for this. > >> > >> Wow, I didn't even consider that.. Thanks! > > also, you can use this: > > > > import std.conv : to; > > ... > > mixin S!(to!string(Foo.BAR)); > > > > people tend to forget that many `to!` variants are usable in > > CTFE. > > Seems like I'd want to move the converting-to-string > functionality to within the template mixin then, something like: > > --- > mixin template S(T, T t) if (is(T == enum)) > { > //import std.traits; > //enum s = fullyQualifiedName!(t); // unfortunately this results > in "t" > > import std.conv: to; > enum s = to!string(t); // this works > } > > mixin S!(Foo, Foo.BAR); > --- > > But passing an enum as parameter seems to be somewhat annoying. > If I leave off the first Foo, then it complains about no-matching > template for int parameter. > !(Foo, Foo.BAR) seems kinda redundant.. something like this? mixin template S(alias fld) if (is(typeof(fld) == enum)) { import std.conv : to; enum s = to!string(fld); // "BAR" // or this: //import std.traits : fullyQualifiedName; //enum s = to!string(fullyQualifiedName!(fld)); // "test.Foo.BAR" } enum Foo { BAR, } void main() { mixin S!(Foo.BAR); } signature.asc Description: PGP signature
Re: Template mixin enum stringof
On Wednesday, 10 December 2014 at 12:57:07 UTC, Daniel Kozák via Digitalmars-d-learn wrote: V Wed, 10 Dec 2014 12:35:44 + Lemonfiend via Digitalmars-d-learn napsáno: On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: > On Wed, 10 Dec 2014 11:52:11 + > Lemonfiend via Digitalmars-d-learn > > wrote: > >> Consider the following: >> >> --- >> enum Foo { BAR, } >> >> mixin template S(string s_) >> { >>enum s = s_; >> } >> >> void main() >> { >>mixin S!(Foo.BAR.stringof); // Error: identifier >> 'stringof' of 'Foo.BAR.stringof' is not defined >> >> enum e = Foo.BAR.stringof; >>mixin S!(e); // works fine >> } >> --- >> >> Why doesn't the first work? And is there an alternative to >> the second version? > > mixin S!((Foo.BAR).stringof); > > sorry, don't remember the parsing rule for this. Wow, I didn't even consider that.. Thanks! Note: Using .stringof for code generation is not recommended, as the internal representation of a type or expression can change between different compiler versions. http://dlang.org/property#stringof Ah, thanks for the warning!
Re: Template mixin enum stringof
On Wednesday, 10 December 2014 at 12:57:16 UTC, ketmar via Digitalmars-d-learn wrote: On Wed, 10 Dec 2014 12:35:44 + Lemonfiend via Digitalmars-d-learn wrote: On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: > On Wed, 10 Dec 2014 11:52:11 + > Lemonfiend via Digitalmars-d-learn > > wrote: > >> Consider the following: >> >> --- >> enum Foo { BAR, } >> >> mixin template S(string s_) >> { >>enum s = s_; >> } >> >> void main() >> { >> mixin S!(Foo.BAR.stringof); // Error: identifier >> 'stringof' of 'Foo.BAR.stringof' is not defined >> >> enum e = Foo.BAR.stringof; >>mixin S!(e); // works fine >> } >> --- >> >> Why doesn't the first work? And is there an alternative to >> the second version? > > mixin S!((Foo.BAR).stringof); > > sorry, don't remember the parsing rule for this. Wow, I didn't even consider that.. Thanks! also, you can use this: import std.conv : to; ... mixin S!(to!string(Foo.BAR)); people tend to forget that many `to!` variants are usable in CTFE. Seems like I'd want to move the converting-to-string functionality to within the template mixin then, something like: --- mixin template S(T, T t) if (is(T == enum)) { //import std.traits; //enum s = fullyQualifiedName!(t); // unfortunately this results in "t" import std.conv: to; enum s = to!string(t); // this works } mixin S!(Foo, Foo.BAR); --- But passing an enum as parameter seems to be somewhat annoying. If I leave off the first Foo, then it complains about no-matching template for int parameter. !(Foo, Foo.BAR) seems kinda redundant..
Re: Template mixin enum stringof
On Wed, 10 Dec 2014 12:35:44 + Lemonfiend via Digitalmars-d-learn wrote: > On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via > Digitalmars-d-learn wrote: > > On Wed, 10 Dec 2014 11:52:11 + > > Lemonfiend via Digitalmars-d-learn > > > > wrote: > > > >> Consider the following: > >> > >> --- > >> enum Foo { BAR, } > >> > >> mixin template S(string s_) > >> { > >>enum s = s_; > >> } > >> > >> void main() > >> { > >>mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof' > >> of 'Foo.BAR.stringof' is not defined > >> > >>enum e = Foo.BAR.stringof; > >>mixin S!(e); // works fine > >> } > >> --- > >> > >> Why doesn't the first work? And is there an alternative to the > >> second version? > > > > mixin S!((Foo.BAR).stringof); > > > > sorry, don't remember the parsing rule for this. > > Wow, I didn't even consider that.. Thanks! also, you can use this: import std.conv : to; ... mixin S!(to!string(Foo.BAR)); people tend to forget that many `to!` variants are usable in CTFE. signature.asc Description: PGP signature
Re: Template mixin enum stringof
V Wed, 10 Dec 2014 12:35:44 + Lemonfiend via Digitalmars-d-learn napsáno: > On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via > Digitalmars-d-learn wrote: > > On Wed, 10 Dec 2014 11:52:11 + > > Lemonfiend via Digitalmars-d-learn > > > > wrote: > > > >> Consider the following: > >> > >> --- > >> enum Foo { BAR, } > >> > >> mixin template S(string s_) > >> { > >>enum s = s_; > >> } > >> > >> void main() > >> { > >>mixin S!(Foo.BAR.stringof); // Error: identifier > >> 'stringof' of 'Foo.BAR.stringof' is not defined > >> > >>enum e = Foo.BAR.stringof; > >>mixin S!(e); // works fine > >> } > >> --- > >> > >> Why doesn't the first work? And is there an alternative to the > >> second version? > > > > mixin S!((Foo.BAR).stringof); > > > > sorry, don't remember the parsing rule for this. > > Wow, I didn't even consider that.. Thanks! Note: Using .stringof for code generation is not recommended, as the internal representation of a type or expression can change between different compiler versions. http://dlang.org/property#stringof
Re: Template mixin enum stringof
V Wed, 10 Dec 2014 11:52:11 + Lemonfiend via Digitalmars-d-learn napsáno: > Consider the following: > > --- > enum Foo { BAR, } > > mixin template S(string s_) > { > enum s = s_; > } > > void main() > { > mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof' > of 'Foo.BAR.stringof' is not defined > > enum e = Foo.BAR.stringof; > mixin S!(e); // works fine > } > --- > > Why doesn't the first work? And is there an alternative to the > second version? import std.traits; enum Foo { BAR, } mixin template S(string s_) { enum s = s_; } void main() { mixin S!(fullyQualifiedName!(Foo.BAR)); enum e = fullyQualifiedName!(Foo.BAR); mixin S!(e); // works fine }
Re: Template mixin enum stringof
On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: On Wed, 10 Dec 2014 11:52:11 + Lemonfiend via Digitalmars-d-learn wrote: Consider the following: --- enum Foo { BAR, } mixin template S(string s_) { enum s = s_; } void main() { mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof' of 'Foo.BAR.stringof' is not defined enum e = Foo.BAR.stringof; mixin S!(e); // works fine } --- Why doesn't the first work? And is there an alternative to the second version? mixin S!((Foo.BAR).stringof); sorry, don't remember the parsing rule for this. Wow, I didn't even consider that.. Thanks!
Re: Template mixin enum stringof
On Wed, 10 Dec 2014 11:52:11 + Lemonfiend via Digitalmars-d-learn wrote: > Consider the following: > > --- > enum Foo { BAR, } > > mixin template S(string s_) > { > enum s = s_; > } > > void main() > { > mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof' of > 'Foo.BAR.stringof' is not defined > > enum e = Foo.BAR.stringof; > mixin S!(e); // works fine > } > --- > > Why doesn't the first work? And is there an alternative to the > second version? mixin S!((Foo.BAR).stringof); sorry, don't remember the parsing rule for this. signature.asc Description: PGP signature
Template mixin enum stringof
Consider the following: --- enum Foo { BAR, } mixin template S(string s_) { enum s = s_; } void main() { mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof' of 'Foo.BAR.stringof' is not defined enum e = Foo.BAR.stringof; mixin S!(e); // works fine } --- Why doesn't the first work? And is there an alternative to the second version?