digitalmars.D.learn - alias and mixin
- evilrat (7/7) Aug 31 2014 what the problem with this?
- ketmar via Digitalmars-d-learn (5/6) Aug 31 2014 On Sun, 31 Aug 2014 11:26:47 +0000
- evilrat (4/11) Aug 31 2014 wow, it works. i don't even think inverting it O_o
- Dicebot (3/16) Aug 31 2014 It is basically just an annoying grammar limitation that does not
- Philippe Sigaud via Digitalmars-d-learn (7/9) Aug 31 2014 The usual helper template:
- Dicebot (11/23) Aug 31 2014 I recommend slightly more generic form:
- Philippe Sigaud via Digitalmars-d-learn (4/12) Aug 31 2014 Mainly, I slap them in my code, then slowly migrate them outwards (in
- evilrat (25/38) Sep 01 2014 ugh... somewhat related to original problem, made simple template
- ketmar via Digitalmars-d-learn (25/25) Sep 01 2014 On Mon, 01 Sep 2014 10:57:41 +0000
- ketmar via Digitalmars-d-learn (27/27) Sep 01 2014 On Mon, 01 Sep 2014 10:57:41 +0000
- ketmar via Digitalmars-d-learn (8/8) Sep 01 2014 On Mon, 01 Sep 2014 10:57:41 +0000
- evilrat (9/21) Sep 01 2014 thanks again, i also made tweaks to my original template to check
- ketmar via Digitalmars-d-learn (4/5) Sep 01 2014 On Mon, 01 Sep 2014 11:56:33 +0000
what the problem with this? alias myint = mixin("int"); // <- basic type expected blah blah blah... mixin alias unusable now, it blocks various cool templates and really frustrating(such things make D feels like some cheap limited language), is there any way to tell compiler explicitly use mixin result for aliasing like above?
Aug 31 2014
On Sun, 31 Aug 2014 11:26:47 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:alias myint =3D mixin("int"); // <- basic type expected blah blah=20mixin("alias myint =3D "~"int"~";"); ?
Aug 31 2014
On Sunday, 31 August 2014 at 11:43:03 UTC, ketmar via Digitalmars-d-learn wrote:On Sun, 31 Aug 2014 11:26:47 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:wow, it works. i don't even think inverting it O_o you saved my day, thanks.alias myint = mixin("int"); // <- basic type expected blah blahmixin("alias myint = "~"int"~";"); ?
Aug 31 2014
On Sunday, 31 August 2014 at 12:01:43 UTC, evilrat wrote:On Sunday, 31 August 2014 at 11:43:03 UTC, ketmar via Digitalmars-d-learn wrote:It is basically just an annoying grammar limitation that does not allow to use mixin / __traits as an identifier.On Sun, 31 Aug 2014 11:26:47 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:wow, it works. i don't even think inverting it O_o you saved my day, thanks.alias myint = mixin("int"); // <- basic type expected blah blahmixin("alias myint = "~"int"~";"); ?
Aug 31 2014
It is basically just an annoying grammar limitation that does not allow to use mixin / __traits as an identifier.The usual helper template: ``` alias helper(alias a) = a; ``` helps for aliasing __traits and anonymous function templates (x => x+1), but I don't see an easy solution for mixin inside the current language, apart from pushing the mixin outside.
Aug 31 2014
On Sunday, 31 August 2014 at 14:46:00 UTC, Philippe Sigaud via Digitalmars-d-learn wrote:I recommend slightly more generic form: template Alias(T...) if (T.length == 1) { alias Alias = T[0]; } it is quite helpful in templated code to be able to alias _anything_ But yeah, no fun for mixins :(It is basically just an annoying grammar limitation that does not allow to use mixin / __traits as an identifier.The usual helper template: ``` alias helper(alias a) = a; ``` helps for aliasing __traits and anonymous function templates (x => x+1), but I don't see an easy solution for mixin inside the current language, apart from pushing the mixin outside.
Aug 31 2014
I recommend slightly more generic form: template Alias(T...) if (T.length == 1) { alias Alias = T[0]; } it is quite helpful in templated code to be able to alias _anything_That's what I use also, but didn't want another thread on the (T...) if (T.length ==1) trick :)But yeah, no fun for mixins :(Mainly, I slap them in my code, then slowly migrate them outwards (in 'head' position) until that compiles...
Aug 31 2014
On Sunday, 31 August 2014 at 12:01:43 UTC, evilrat wrote:On Sunday, 31 August 2014 at 11:43:03 UTC, ketmar via Digitalmars-d-learn wrote:ugh... somewhat related to original problem, made simple template for quick check if type present or provide alternative. now that is something i am not understand. sorry for my stupidness :( --- code struct A {} alias coolStruct = typeByName!("MyStruct", A); // oops, looks like alias is set to template itself, though // code completion(mono-d) shows it is correctly aliased to A // // error: template instance is used as a type void doSomething(coolStruct cs) { .. do something with struct .. } --- template itself template typeByName(alias Name, Replace) { static if ( __traits(compiles, (mixin(Name~`.sizeof`))) ) mixin(`alias typeByName = ` ~ Name ~ `;`); else static if ( (is(Replace == class) || is(Replace == struct)) ) alias typeByName = Replace; }On Sun, 31 Aug 2014 11:26:47 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:wow, it works. i don't even think inverting it O_o you saved my day, thanks.alias myint = mixin("int"); // <- basic type expected blah blahmixin("alias myint = "~"int"~";"); ?
Sep 01 2014
On Mon, 01 Sep 2014 10:57:41 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: here's some more ugly hackery for you: string makeAlias(string aliasName, string Name, alias Replace) () { static if ( __traits(compiles, (mixin(Name~`.sizeof`))) ) return `alias `~aliasName~` =3D ` ~ Name ~ `;`; static if ( (is(Replace =3D=3D class) || is(Replace =3D=3D struct)) ) return `alias `~aliasName~` =3D ` ~ Replace.stringof ~ `;`; assert(0); } struct A {} mixin(makeAlias!("coolStruct", "MyStruct", A)); or: mixin template makeAlias(string aliasName, string Name, alias Replace) { static if ( __traits(compiles, (mixin(Name~`.sizeof`))) ) mixin(`alias `~aliasName~` =3D ` ~ Name ~ `;`); static if ( (is(Replace =3D=3D class) || is(Replace =3D=3D struct)) ) mixin(`alias `~aliasName~` =3D ` ~ Replace.stringof ~ `;`); else static assert(0); } struct A {} mixin makeAlias!("coolStruct", "MyStruct", A);
Sep 01 2014
On Mon, 01 Sep 2014 10:57:41 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: and to check that ugly hackery: mixin template makeAlias(string aliasName, string Name, alias Replace) { static if ( __traits(compiles, (mixin(Name~`.sizeof`))) ) mixin(`alias `~aliasName~` =3D ` ~ Name ~ `;`); static if ( (is(Replace =3D=3D class) || is(Replace =3D=3D struct)) ) mixin(`alias `~aliasName~` =3D ` ~ Replace.stringof ~ `;`); else static assert(0); } struct A {} struct B {} struct C {} mixin makeAlias!("coolStruct", "MyStruct", A); mixin makeAlias!("coolStruct1", "B", C); void doSomething(coolStruct cs) { static if (!is(typeof(cs) =3D=3D A)) static assert(0); //.. do something with struct .. } void doSomething1(coolStruct1 cs) { static if (!is(typeof(cs) =3D=3D B)) static assert(0); //.. do something with struct .. }
Sep 01 2014
On Mon, 01 Sep 2014 10:57:41 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: p.s. you should really read about D mixins and templates. they aren't such intuitive sometimes, has their set of funny restrictions, and it may be hard to 'guess the right way' sometimes. but to be honest, i wasn't read any books and found alot of things by experimenting, so it's not a dead-end way too. ;-)
Sep 01 2014
On Monday, 1 September 2014 at 11:23:37 UTC, ketmar via Digitalmars-d-learn wrote:On Mon, 01 Sep 2014 10:57:41 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: p.s. you should really read about D mixins and templates. they aren't such intuitive sometimes, has their set of funny restrictions, and it may be hard to 'guess the right way' sometimes. but to be honest, i wasn't read any books and found alot of things by experimenting, so it's not a dead-end way too. ;-)thanks again, i also made tweaks to my original template to check for aliases and now it works too, means that error message was totally misleading. btw i had read some articles about mixins and templates but still difference between template and mixin template is somewhat confusing. and D states "intuitive is the way", yeah...
Sep 01 2014
On Mon, 01 Sep 2014 11:56:33 +0000 evilrat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:and D states "intuitive is the way", yeah...it's still way better than C++. ;-)
Sep 01 2014