www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why not mixin "int a;" ??

reply =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
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
Aug 24 2008
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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..
Aug 24 2008
parent reply =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
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
Aug 24 2008
parent reply BCS <ao pathlink.com> writes:
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..
 

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?
Aug 24 2008
parent =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
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
Aug 24 2008
prev sibling parent reply =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
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
Aug 24 2008
next sibling parent reply BCS <ao pathlink.com> writes:
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.
Aug 24 2008
parent reply =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
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
Aug 24 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Tomasz,

 BCS Wrote:
 
 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.
 

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
Aug 24 2008
parent reply =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
BCS Wrote:

 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
 

never mind, knowing that it's a bug helps, because I won't waste time trying to fix it. Thanks
Aug 25 2008
parent BCS <ao pathlink.com> writes:
Reply to Tomasz,

 BCS Wrote:
 
 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
 

trying to fix it. Thanks

I'm not saying it's a bug (or that it is not), closed bug's would contain examples of things that DMD can now do.
Aug 25 2008
prev sibling parent reply Max Samukha <samukha voliacable.com.removethis> writes:
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) }
Aug 24 2008
parent =?ISO-8859-1?Q?Tomasz_Sowi=f1ski?= <tomeksowi gmail.com> writes:
Max Samukha Wrote:
 
 I think you tried to pass separate strings to the non-variadic
 version. The following works:
 

works! thank you
Aug 25 2008