Re: alias and mixin

2014-09-01 Thread evilrat via Digitalmars-d-learn

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 +
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;
}


Re: alias and mixin

2014-09-01 Thread ketmar via Digitalmars-d-learn
On Mon, 01 Sep 2014 10:57:41 +
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);


signature.asc
Description: PGP signature


Re: alias and mixin

2014-09-01 Thread ketmar via Digitalmars-d-learn
On Mon, 01 Sep 2014 10:57:41 +
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 ..
}


signature.asc
Description: PGP signature


Re: alias and mixin

2014-09-01 Thread ketmar via Digitalmars-d-learn
On Mon, 01 Sep 2014 10:57:41 +
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. ;-)


signature.asc
Description: PGP signature


Re: alias and mixin

2014-09-01 Thread evilrat via Digitalmars-d-learn
On Monday, 1 September 2014 at 11:23:37 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Mon, 01 Sep 2014 10:57:41 +
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...


Re: alias and mixin

2014-09-01 Thread ketmar via Digitalmars-d-learn
On Mon, 01 Sep 2014 11:56:33 +
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++. ;-)


signature.asc
Description: PGP signature


Re: alias and mixin

2014-08-31 Thread ketmar via Digitalmars-d-learn
On Sun, 31 Aug 2014 11:26:47 +
evilrat via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 alias myint = mixin(int); // - basic type expected blah blah 
  mixin(alias myint = ~int~;);
?


signature.asc
Description: PGP signature


Re: alias and mixin

2014-08-31 Thread evilrat via Digitalmars-d-learn
On Sunday, 31 August 2014 at 11:43:03 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sun, 31 Aug 2014 11:26:47 +
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.



Re: alias and mixin

2014-08-31 Thread Dicebot via Digitalmars-d-learn

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 +
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.


Re: alias and mixin

2014-08-31 Thread Philippe Sigaud via Digitalmars-d-learn
 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.


Re: alias and mixin

2014-08-31 Thread Dicebot via Digitalmars-d-learn
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 :(


Re: alias and mixin

2014-08-31 Thread Philippe Sigaud via Digitalmars-d-learn
 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...


Re: alias c=mixin(expr); disallowed, why?

2013-06-23 Thread Jacob Carlborg

On 2013-06-22 23:47, Timon Gehr wrote:


No, it is arbitrary.


I think the spec says you can only mixin whole expression. But for some 
reason you can use a mixin in a __traits expression without having the 
whole expression in a mixin.


--
/Jacob Carlborg


Re: alias c=mixin(expr); disallowed, why?

2013-06-23 Thread Artur Skawina
On 06/22/13 21:52, Timothee Cour wrote:
 Is there a reason the language spec disallows this?
 
 
 void main(){
 auto a=mixin(1);//OK
 alias b=a;//OK
 mixin(alias c=a;);//OK
 // alias c=mixin(a);//NG : Error: basic type expected, not mixin
 }

How would that be different from auto c=mixin(a);?

It's probably clear, but that error message is misleading, so i'll say
it anyway - the reason why your 'alias' line does not work is because
alias requires a symbol, but 'mixin()' is an expression.
Special-casing mixin-expressions (so that they propagate the symbol when
that is possible would be a bad idea); the other possibility is to allow
aliasing /expressions/. But that's a bad idea too, and would likely not
do what you expect it to do. A mixin-less version could be made to work,
but there are already other ways to get the same effect.
Hence the above question.

artur


Re: alias c=mixin(expr); disallowed, why?

2013-06-23 Thread Artur Skawina
On 06/23/13 13:23, Timon Gehr wrote:
 On 06/23/2013 12:19 PM, Artur Skawina wrote:
 On 06/22/13 21:52, Timothee Cour wrote:
 Is there a reason the language spec disallows this?

 
 void main(){
 auto a=mixin(1);//OK
 alias b=a;//OK
 mixin(alias c=a;);//OK
 // alias c=mixin(a);//NG : Error: basic type expected, not mixin
 }

 How would that be different from auto c=mixin(a);?

 It's probably clear, but that error message is misleading, so i'll say
 it anyway - the reason why your 'alias' line does not work is because
 alias requires a symbol, but 'mixin()' is an expression.
 Special-casing mixin-expressions (so that they propagate the symbol when
 that is possible would be a bad idea); the other possibility is to allow
 aliasing /expressions/. But that's a bad idea too, and would likely not
 do what you expect it to do. A mixin-less version could be made to work,
 but there are already other ways to get the same effect.
 Hence the above question.
 
 mixin template T(alias x){ }
 
 void foo(){ import std.stdio; writeln(foo); }
 
 void main(){
 auto a=mixin(1);
 mixin T!a; // ok
 mixin T!(mixin(a)); // ok
 mixin T!1; // ok (!)
 mixin T!(mixin(foo)); // ok (no implicit call)
 }

Yes, template parms and literals are special (literals are useful as template
parms, obviously).

What would be the arguments for allowing these two:

 mixin T!(mixin(a)); // ok
 mixin T!(mixin(foo)); // ok (no implicit call)

?
I didn't realize they were currently accepted. Is this actually
documented somewhere? The fact that `mixin(foo)` and `mixin(foo+1)`
mean subtly different things when used as a template parameter is a problem.

artur


Re: alias c=mixin(expr); disallowed, why?

2013-06-23 Thread Jacob Carlborg

On 2013-06-23 13:27, Timon Gehr wrote:


Example?


@(3) int a;
alias Tuple!(__traits(getAttributes, mixin(a))) attrs;

--
/Jacob Carlborg


Re: alias c=mixin(expr); disallowed, why?

2013-06-22 Thread Timon Gehr

On 06/22/2013 09:52 PM, Timothee Cour wrote:

Is there a reason the language spec disallows this?


void main(){
auto a=mixin(1);//OK
alias b=a;//OK
mixin(alias c=a;);//OK
// alias c=mixin(a);//NG : Error: basic type expected, not mixin
}



No, it is arbitrary.


Re: alias c=mixin(expr); disallowed, why?

2013-06-22 Thread Timothee Cour
On Sat, Jun 22, 2013 at 2:47 PM, Timon Gehr timon.g...@gmx.ch wrote:

 On 06/22/2013 09:52 PM, Timothee Cour wrote:

 Is there a reason the language spec disallows this?

 
 void main(){
 auto a=mixin(1);//OK
 alias b=a;//OK
 mixin(alias c=a;);//OK
 // alias c=mixin(a);//NG : Error: basic type expected, not mixin
 }
 


 No, it is arbitrary.


that's what i thought!
same with template mixin's being restricated.

... can we fix it?

it makes for cleaner code.


Re: alias c=mixin(expr); disallowed, why?

2013-06-22 Thread Timon Gehr

On 06/22/2013 11:51 PM, Timothee Cour wrote:



On Sat, Jun 22, 2013 at 2:47 PM, Timon Gehr timon.g...@gmx.ch
mailto:timon.g...@gmx.ch wrote:

On 06/22/2013 09:52 PM, Timothee Cour wrote:

Is there a reason the language spec disallows this?


void main(){
 auto a=mixin(1);//OK
 alias b=a;//OK
 mixin(alias c=a;);//OK
 // alias c=mixin(a);//NG : Error: basic type expected,
not mixin
}



No, it is arbitrary.


that's what i thought!
same with template mixin's being restricated.



?


... can we fix it?

it makes for cleaner code.



It surely could be fixed if you want to spend the time hacking on DMD.
I think the grammar is somewhat poorly designed in general.


Re: alias c=mixin(expr); disallowed, why?

2013-06-22 Thread Timothee Cour
On Sat, Jun 22, 2013 at 2:55 PM, Timon Gehr timon.g...@gmx.ch wrote:

 On 06/22/2013 11:51 PM, Timothee Cour wrote:



 On Sat, Jun 22, 2013 at 2:47 PM, Timon Gehr timon.g...@gmx.ch
 mailto:timon.g...@gmx.ch wrote:

 On 06/22/2013 09:52 PM, Timothee Cour wrote:

 Is there a reason the language spec disallows this?

 
 void main(){
  auto a=mixin(1);//OK
  alias b=a;//OK
  mixin(alias c=a;);//OK
  // alias c=mixin(a);//NG : Error: basic type expected,
 not mixin
 }
 


 No, it is arbitrary.


 that's what i thought!
 same with template mixin's being restricated.


 ?


they're restricted to declarations. I don't see the need for such
restriction.



  ... can we fix it?

 it makes for cleaner code.


 It surely could be fixed if you want to spend the time hacking on DMD.
 I think the grammar is somewhat poorly designed in general.