www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias and mixin

reply "evilrat" <evilrat666 gmail.com> writes:
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
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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=20
mixin("alias myint =3D "~"int"~";"); ?
Aug 31 2014
parent reply "evilrat" <evilrat666 gmail.com> writes:
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:

 alias myint = mixin("int"); // <- basic type expected blah blah
mixin("alias myint = "~"int"~";"); ?
wow, it works. i don't even think inverting it O_o you saved my day, thanks.
Aug 31 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
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:
 On Sun, 31 Aug 2014 11:26:47 +0000
 evilrat via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 alias myint = mixin("int"); // <- basic type expected blah 
 blah
mixin("alias myint = "~"int"~";"); ?
wow, it works. i don't even think inverting it O_o you saved my day, thanks.
It is basically just an annoying grammar limitation that does not allow to use mixin / __traits as an identifier.
Aug 31 2014
parent reply Philippe Sigaud via Digitalmars-d-learn writes:
 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
parent reply "Dicebot" <public dicebot.lv> writes:
On Sunday, 31 August 2014 at 14:46:00 UTC, Philippe Sigaud via 
Digitalmars-d-learn wrote:
 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.
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 :(
Aug 31 2014
parent Philippe Sigaud via Digitalmars-d-learn writes:
 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
prev sibling parent reply "evilrat" <evilrat666 gmail.com> writes:
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:
 On Sun, 31 Aug 2014 11:26:47 +0000
 evilrat via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 alias myint = mixin("int"); // <- basic type expected blah 
 blah
mixin("alias myint = "~"int"~";"); ?
wow, it works. i don't even think inverting it O_o you saved my day, thanks.
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; }
Sep 01 2014
next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
parent reply "evilrat" <evilrat666 gmail.com> writes:
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
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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