Jump to page: 1 2
Thread overview
alias and mixin
Aug 31, 2014
evilrat
Aug 31, 2014
ketmar
Aug 31, 2014
evilrat
Aug 31, 2014
Dicebot
Aug 31, 2014
Philippe Sigaud
Aug 31, 2014
Dicebot
Aug 31, 2014
Philippe Sigaud
Sep 01, 2014
evilrat
Sep 01, 2014
ketmar
Sep 01, 2014
ketmar
Sep 01, 2014
ketmar
Sep 01, 2014
evilrat
Sep 01, 2014
ketmar
August 31, 2014
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?
August 31, 2014
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"~";");
?


August 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:
>
>> 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.

August 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:
>> 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.
August 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.
August 31, 2014
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 :(
August 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...
September 01, 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:
>> 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;
}
September 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~` = ` ~ Name ~ `;`;
      static if ( (is(Replace == class) || is(Replace == struct)) )
        return `alias `~aliasName~` = ` ~ 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~` = ` ~ Name ~ `;`);
      static if ( (is(Replace == class) || is(Replace == struct)) )
        mixin(`alias `~aliasName~` = ` ~ Replace.stringof ~ `;`);
      else static assert(0);
    }


    struct A {}
    mixin makeAlias!("coolStruct", "MyStruct", A);


September 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~` = ` ~ Name ~ `;`);
      static if ( (is(Replace == class) || is(Replace == struct)) )
        mixin(`alias `~aliasName~` = ` ~ 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) == A)) static assert(0);
      //.. do something with struct ..
    }

    void doSomething1(coolStruct1 cs)
    {
      static if (!is(typeof(cs) == B)) static assert(0);
      //.. do something with struct ..
    }


« First   ‹ Prev
1 2