Thread overview | |||||
---|---|---|---|---|---|
|
June 23, 2013 Re: alias c=mixin(expr); disallowed, why? | ||||
---|---|---|---|---|
| ||||
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
|
June 23, 2013 Re: alias c=mixin(expr); disallowed, why? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | 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.
>
> artur
>
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)
}
|
June 23, 2013 Re: alias c=mixin(expr); disallowed, why? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 |
Copyright © 1999-2021 by the D Language Foundation