digitalmars.D.learn - Template mixin enum stringof
- Lemonfiend (17/17) Dec 10 2014 Consider the following:
- ketmar via Digitalmars-d-learn (5/27) Dec 10 2014 On Wed, 10 Dec 2014 11:52:11 +0000
- Lemonfiend (3/31) Dec 10 2014 Wow, I didn't even consider that.. Thanks!
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (7/42) Dec 10 2014 V Wed, 10 Dec 2014 12:35:44 +0000
- Lemonfiend (3/48) Dec 10 2014 Ah, thanks for the warning!
- ketmar via Digitalmars-d-learn (8/43) Dec 10 2014 On Wed, 10 Dec 2014 12:35:44 +0000
- Lemonfiend (19/64) Dec 10 2014 Seems like I'd want to move the converting-to-string
- ketmar via Digitalmars-d-learn (17/89) Dec 10 2014 On Wed, 10 Dec 2014 13:58:20 +0000
- Lemonfiend (3/95) Dec 10 2014 Perfect, thanks.
- ketmar via Digitalmars-d-learn (5/23) Dec 10 2014 On Wed, 10 Dec 2014 14:32:12 +0000
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (16/38) Dec 10 2014 V Wed, 10 Dec 2014 11:52:11 +0000
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?
Dec 10 2014
On Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Consider the following: =20 --- enum Foo { BAR, } =20 mixin template S(string s_) { enum s =3D s_; } =20 void main() { mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof' of=20 'Foo.BAR.stringof' is not defined =20 enum e =3D Foo.BAR.stringof; mixin S!(e); // works fine } --- =20 Why doesn't the first work? And is there an alternative to the=20 second version?mixin S!((Foo.BAR).stringof); sorry, don't remember the parsing rule for this.
Dec 10 2014
On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote:On Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Wow, I didn't even consider that.. Thanks!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.
Dec 10 2014
V Wed, 10 Dec 2014 12:35:44 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: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#stringofOn Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Wow, I didn't even consider that.. Thanks!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.
Dec 10 2014
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 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> napsáno:Ah, thanks for the warning!On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: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#stringofOn Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Wow, I didn't even consider that.. Thanks!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.
Dec 10 2014
On Wed, 10 Dec 2014 12:35:44 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via=20 Digitalmars-d-learn wrote: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.On Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn=20 <digitalmars-d-learn puremagic.com> wrote:=20 Wow, I didn't even consider that.. Thanks!Consider the following: =20 --- enum Foo { BAR, } =20 mixin template S(string s_) { enum s =3D s_; } =20 void main() { mixin S!(Foo.BAR.stringof); // Error: identifier 'stringof'=20 of 'Foo.BAR.stringof' is not defined =20 enum e =3D Foo.BAR.stringof; mixin S!(e); // works fine } --- =20 Why doesn't the first work? And is there an alternative to the=20 second version?mixin S!((Foo.BAR).stringof); sorry, don't remember the parsing rule for this.
Dec 10 2014
On Wednesday, 10 December 2014 at 12:57:16 UTC, ketmar via Digitalmars-d-learn wrote:On Wed, 10 Dec 2014 12:35:44 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: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..On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: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.On Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Wow, I didn't even consider that.. Thanks!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.
Dec 10 2014
On Wed, 10 Dec 2014 13:58:20 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Wednesday, 10 December 2014 at 12:57:16 UTC, ketmar via=20 Digitalmars-d-learn wrote:something like this? mixin template S(alias fld) if (is(typeof(fld) =3D=3D enum)) { import std.conv : to; enum s =3D to!string(fld); // "BAR" // or this: //import std.traits : fullyQualifiedName; //enum s =3D to!string(fullyQualifiedName!(fld)); // "test.Foo.BAR" } enum Foo { BAR, } void main() { mixin S!(Foo.BAR); }On Wed, 10 Dec 2014 12:35:44 +0000 Lemonfiend via Digitalmars-d-learn=20 <digitalmars-d-learn puremagic.com> wrote:=20 Seems like I'd want to move the converting-to-string=20 functionality to within the template mixin then, something like: =20 --- mixin template S(T, T t) if (is(T =3D=3D enum)) { //import std.traits; //enum s =3D fullyQualifiedName!(t); // unfortunately this results=20 in "t" =20 import std.conv: to; enum s =3D to!string(t); // this works } =20 mixin S!(Foo, Foo.BAR); --- =20 But passing an enum as parameter seems to be somewhat annoying.=20 If I leave off the first Foo, then it complains about no-matching=20 template for int parameter. !(Foo, Foo.BAR) seems kinda redundant..On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via=20 Digitalmars-d-learn wrote: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=20 CTFE.On Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn=20 <digitalmars-d-learn puremagic.com> wrote:=20 Wow, I didn't even consider that.. Thanks!Consider the following: =20 --- enum Foo { BAR, } =20 mixin template S(string s_) { enum s =3D s_; } =20 void main() { mixin S!(Foo.BAR.stringof); // Error: identifier=20 'stringof' of 'Foo.BAR.stringof' is not defined =20 enum e =3D Foo.BAR.stringof; mixin S!(e); // works fine } --- =20 Why doesn't the first work? And is there an alternative to=20 the second version?mixin S!((Foo.BAR).stringof); sorry, don't remember the parsing rule for this.
Dec 10 2014
On Wednesday, 10 December 2014 at 14:25:30 UTC, ketmar via Digitalmars-d-learn wrote:On Wed, 10 Dec 2014 13:58:20 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Perfect, thanks.On Wednesday, 10 December 2014 at 12:57:16 UTC, ketmar via Digitalmars-d-learn wrote: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); }On Wed, 10 Dec 2014 12:35:44 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: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..On Wednesday, 10 December 2014 at 12:08:34 UTC, ketmar via Digitalmars-d-learn wrote: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.On Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Wow, I didn't even consider that.. Thanks!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.
Dec 10 2014
On Wed, 10 Dec 2014 14:32:12 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:p.s. be careful with imports, as they will go to the same scope as `s`. this may or may not be important.mixin template S(alias fld) if (is(typeof(fld) =3D=3D enum)) { import std.conv : to; enum s =3D to!string(fld); // "BAR" // or this: //import std.traits : fullyQualifiedName; //enum s =3D to!string(fullyQualifiedName!(fld)); //=20 "test.Foo.BAR" } enum Foo { BAR, } void main() { mixin S!(Foo.BAR); }=20 Perfect, thanks.
Dec 10 2014
V Wed, 10 Dec 2014 11:52:11 +0000 Lemonfiend via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> 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 }
Dec 10 2014