Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 24, 2008 Why not mixin "int a;" ?? | ||||
---|---|---|---|---|
| ||||
I can mixin templates without parens, like this: mixin foo!(int); So why can't I do this: mixin "int a;"; instead of this: mixin("int a;"); ? Seems a bit inconsistent to me, but maybe I don't see the reason... Tomek |
August 24, 2008 Re: Why not mixin "int a;" ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomasz Sowiñski | "Tomasz Sowiñski" <tomeksowi@gmail.com> wrote in message news:g8rnkc$1fj5$1@digitalmars.com... >I can mixin templates without parens, like this: > mixin foo!(int); > > So why can't I do this: > mixin "int a;"; > instead of this: > mixin("int a;"); > ? > > Seems a bit inconsistent to me, but maybe I don't see the reason... > > Tomek mixin foo; This is now grammatically ambiguous. If foo is a const char[], then it's a string mixin. If foo is a template, then it's the same as "mixin foo!();". This could be disambiguated later, but.. |
August 24, 2008 Re: Why not mixin "int a;" ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> mixin foo;
>
> This is now grammatically ambiguous. If foo is a const char[], then it's a string mixin. If foo is a template, then it's the same as "mixin foo!();". This could be disambiguated later, but..
>
Is there a difference in what happens when you mixin a template and a string? I still don't see why this distinction is necessary.
Tomek
|
August 24, 2008 Re: Why not mixin "int a;" ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomasz Sowiñski | Reply to Tomasz,
> Jarrett Billingsley Wrote:
>
>> mixin foo;
>>
>> This is now grammatically ambiguous. If foo is a const char[], then
>> it's a string mixin. If foo is a template, then it's the same as
>> "mixin foo!();". This could be disambiguated later, but..
>>
> Is there a difference in what happens when you mixin a template and a
> string? I still don't see why this distinction is necessary.
>
> Tomek
>
template Foo()
{
static const char[] Foo = "int a;"
}
mixin Foo;
did that add a var named a or a const named Foo?
|
August 24, 2008 One more about mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomasz Sowiñski | I got a code generating function: string intGen(string[] names...) { string result; foreach (name; names) result ~= "int " ~ name ~ "; "; return result; } void main() { writeln(intGen("aa", "bb", "cc")); // ok, writes "int aa; int bb; int cc; " } struct tag { mixin(intGen("aa", "bb", "cc")); // doesn't compile, but why? } I tried using a string[] as an argument instead of a variadic argument, but still it doesn't work. I know the compiler must be able to evaluate mixins at compile time and I think it's able to do so in the example. Tomek |
August 24, 2008 Re: One more about mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomasz Sowiñski | Reply to Tomasz,
> I got a code generating function:
>
> string intGen(string[] names...)
> {
> string result;
> foreach (name; names)
> result ~= "int " ~ name ~ "; ";
> return result;
> }
> void main() {
> writeln(intGen("aa", "bb", "cc")); // ok, writes "int aa; int bb;
> int cc; "
> }
> struct tag {
> mixin(intGen("aa", "bb", "cc")); // doesn't compile, but why?
> }
> I tried using a string[] as an argument instead of a variadic
> argument, but still it doesn't work. I know the compiler must be able
> to evaluate mixins at compile time and I think it's able to do so in
> the example.
>
> Tomek
>
using CTFE (compile time function evaluation) with arrays is "finicky"
To limit the problem to the CTFE issues switch to this:
pragma(msg, intGen("aa", "bb", "cc"));
Post the error messages from that if you can't figure them out.
|
August 25, 2008 Re: One more about mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote: > using CTFE (compile time function evaluation) with arrays is "finicky" so I noticed:) > To limit the problem to the CTFE issues switch to this: > > pragma(msg, intGen("aa", "bb", "cc")); > > Post the error messages from that if you can't figure them out. It says intGen("aa", "bb", "cc") can't be evaluated at compile time... I don't see why, all of its arguments are known at compile time and the function doesn't refer to anything outside its body... test.d(13): Error: cannot evaluate intGen(cast(invariant(char)[][])((invariant(char)[][3u] __arrayArg286 = void; ) , (__arrayArg286[0u]) = "aa" , (__arrayArg286[1u]) = "bb" , (__arrayArg286[2u]) = "cc" , __arrayArg286)) at compile time test.d(13): Error: string expected for message, not 'intGen(cast(invariant(char)[][])((invariant(char)[][3u] __arrayArg286 = void; ) , (__arrayArg286[0u]) = "aa" , (__arrayArg286[1u]) = "bb" , (__arrayArg286[2u]) = "cc" , __arrayArg286))' line 13 contains the pragma, of course. Tomek |
August 25, 2008 Re: Why not mixin "int a;" ?? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> template Foo()
> {
> static const char[] Foo = "int a;"
> }
>
> mixin Foo;
>
> did that add a var named a or a const named Foo?
>
a const named Foo
something like "mixin Foo!().Foo;" should add a var named a, no?
btw, I tried "mixin(Foo);" and it doesn't compile: Error: argument to mixin must be a string, not (Foo())
so there wouldn't be any ambiguity anyway
Tomek
|
August 25, 2008 Re: One more about mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomasz Sowiñski | Reply to Tomasz,
> BCS Wrote:
>
>> using CTFE (compile time function evaluation) with arrays is
>> "finicky"
>>
> so I noticed:)
>
>> To limit the problem to the CTFE issues switch to this:
>>
>> pragma(msg, intGen("aa", "bb", "cc"));
>>
>> Post the error messages from that if you can't figure them out.
>>
> It says intGen("aa", "bb", "cc") can't be evaluated at compile
> time... I don't see why, all of its arguments are known at compile
> time and the function doesn't refer to anything outside its body...
>
The CTFE engine is insanely limited and I can't seem to find the docs. You might search in the buzilla for closed bugs with CTFE.
Looking at that error I'm not shure what was happening, sorry
|
August 25, 2008 Re: One more about mixins | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomasz Sowiñski | On Sun, 24 Aug 2008 19:21:54 -0400, Tomasz Sowi?ski <tomeksowi@gmail.com> wrote: > >I tried using a string[] as an argument instead of a variadic argument, but still it doesn't work. I think you tried to pass separate strings to the non-variadic version. The following works: string intGen(string[] names) { string result; foreach (name; names) result ~= "int " ~ name ~ "; "; return result; } struct tag { mixin(intGen(["aa", "bb", "cc"])); } void main() { writefln(tag.tupleof.stringof); // tuple((tag).aa,(tag).bb,(tag).cc) } |
Copyright © 1999-2021 by the D Language Foundation