Thread overview
mixin template FAIL
Feb 21, 2012
Zach the Mystic
Feb 21, 2012
Ali Çehreli
Feb 21, 2012
Jacob Carlborg
Feb 21, 2012
Ellery Newcomer
Feb 23, 2012
Zach the Mystic
Feb 24, 2012
H. S. Teoh
Feb 24, 2012
Ellery Newcomer
Feb 24, 2012
Zach the Mystic
Feb 24, 2012
H. S. Teoh
Feb 25, 2012
James Miller
February 21, 2012
I decided to try using template mixin, but even the simplest program fails. What's wrong with this code? Error list follows.
DMD64 D Compiler v2.057  OSX 10.6

import std.stdio;

mixin template helpMe()
{
   writeln("Satisfying!");
}

void main()
{
   mixin helpMe();
}

test.d(5): unexpected ( in declarator
test.d(5): basic type expected, not "Satisfying!"
test.d(5): found '"Satisfying!"' when expecting ')'
test.d(5): no identifier for declarator writeln(int)
test.d(5): semicolon expected following function declaration
test.d(5): Declaration expected, not ')'
test.d(10): ';' expected after mixin
test.d(10): found ')' instead of statement

February 21, 2012
On 02/21/2012 10:47 AM, Zach the Mystic wrote:
> I decided to try using template mixin, but even the simplest program
> fails. What's wrong with this code? Error list follows.
> DMD64 D Compiler v2.057 OSX 10.6
>
> import std.stdio;
>
> mixin template helpMe()
> {
> writeln("Satisfying!");
> }
>
> void main()
> {
> mixin helpMe();
> }
>
> test.d(5): unexpected ( in declarator
> test.d(5): basic type expected, not "Satisfying!"
> test.d(5): found '"Satisfying!"' when expecting ')'
> test.d(5): no identifier for declarator writeln(int)
> test.d(5): semicolon expected following function declaration
> test.d(5): Declaration expected, not ')'
> test.d(10): ';' expected after mixin
> test.d(10): found ')' instead of statement
>

According to the docs, template mixins can have only declarations but helpMe above has a statement.

  http://dlang.org/template-mixin.html

Ali

February 21, 2012
On 2012-02-21 20:53, Ali Çehreli wrote:
> On 02/21/2012 10:47 AM, Zach the Mystic wrote:
>  > I decided to try using template mixin, but even the simplest program
>  > fails. What's wrong with this code? Error list follows.
>  > DMD64 D Compiler v2.057 OSX 10.6
>  >
>  > import std.stdio;
>  >
>  > mixin template helpMe()
>  > {
>  > writeln("Satisfying!");
>  > }
>  >
>  > void main()
>  > {
>  > mixin helpMe();
>  > }
>  >
>  > test.d(5): unexpected ( in declarator
>  > test.d(5): basic type expected, not "Satisfying!"
>  > test.d(5): found '"Satisfying!"' when expecting ')'
>  > test.d(5): no identifier for declarator writeln(int)
>  > test.d(5): semicolon expected following function declaration
>  > test.d(5): Declaration expected, not ')'
>  > test.d(10): ';' expected after mixin
>  > test.d(10): found ')' instead of statement
>  >
>
> According to the docs, template mixins can have only declarations but
> helpMe above has a statement.
>
> http://dlang.org/template-mixin.html
>
> Ali
>

And the correct syntax for mixing in the template would be:

mixin helpMe!();

Or

mixin helpMe; // works if the template doesn't take any arguments

-- 
/Jacob Carlborg
February 21, 2012
On 02/21/2012 01:53 PM, Ali Çehreli wrote:
>
> According to the docs, template mixins can have only declarations but
> helpMe above has a statement.
>
> http://dlang.org/template-mixin.html
>
> Ali
>

come to think of it, I've occasionally wished for statement mixins. This would make a good enhancement request.

February 23, 2012
On 2/21/12 2:53 PM, Ali Çehreli wrote:
> According to the docs, template mixins can have only declarations but
> helpMe above has a statement.
>
> http://dlang.org/template-mixin.html
>
> Ali
>

Thanks for your reply. You're right about the statement. But I still think something's wrong. For example, even this program produces the errors:

import std.stdio;

mixin template helpMe()
{
   writeln("Satisfying!");
}

void main(){}

test.d(5): unexpected ( in declarator
test.d(5): basic type expected, not "Satisfying!"
test.d(5): found '"Satisfying!"' when expecting ')'
test.d(5): no identifier for declarator writeln(int)
test.d(5): semicolon expected following function declaration
test.d(5): Declaration expected, not ')'

The parser just isn't recognizing the presence of the template mixin format. The errors happen instantly, which generally happens only when you have parser errors. I don't know, but I think there might be some stupid typo in the source code for the current dmd I'm using (2.057 Mac OSX 10.6). I'd have to download another version, but I was distracted by other things.

Zach
February 24, 2012
On Thu, Feb 23, 2012 at 05:27:03PM -0500, Zach the Mystic wrote:
> On 2/21/12 2:53 PM, Ali Çehreli wrote:
> >According to the docs, template mixins can have only declarations but helpMe above has a statement.
> >
> >http://dlang.org/template-mixin.html
> >
> >Ali
> >
> 
> Thanks for your reply. You're right about the statement. But I still think something's wrong. For example, even this program produces the errors:
> 
> import std.stdio;
> 
> mixin template helpMe()
> {
>    writeln("Satisfying!");

The writeln call is a statement.

I think what you want is this:

	template helpMe() {
		mixin(`writeln("Satisfying!");`);
	}


T

-- 
Real Programmers use "cat > a.out".
February 24, 2012
>
> Thanks for your reply. You're right about the statement. But I still
> think something's wrong. For example, even this program produces the
> errors:
>
> import std.stdio;
>
> mixin template helpMe()
> {
> writeln("Satisfying!");
> }
>

does it do that if you replace the statement with a declaration?

like this:

mixin template helpMe()
{
  int durrr = (writeln("Satisfying!"), 1);
}
February 24, 2012
On 2/23/12 7:33 PM, Ellery Newcomer wrote:
>> import std.stdio;
>>
>> mixin template helpMe()
>> {
>> writeln("Satisfying!");
>> }
> does it do that if you replace the statement with a declaration?
>
> like this:
>
> mixin template helpMe()
> {
> int durrr = (writeln("Satisfying!"), 1);
> }

No, it doesn't. You're right. I guess I have a long way to go to learn these things.

Thank you. Even at my primitive level, though, I can see how awesome these things could be once you know how to program them. Does any other language come close to D in terms of generics? I don't know, I'm just asking?

Zach
February 24, 2012
On Fri, Feb 24, 2012 at 03:08:18PM -0500, Zach the Mystic wrote: [...]
> Does any other language come close to D in terms of generics? I don't know, I'm just asking?
[...]

AFAIK, no. But then I only have C++ to compare with, and if I understand it correctly Java and C#'s generics don't even come close to C++'s templates (in terms of expressive power, though they are certainly a lot cleaner than the mess that is C++ template syntax).


T

-- 
Some ideas are so stupid that only intellectuals could believe them. -- George Orwell
February 25, 2012
On Feb 25, 2012 9:08 AM, "Zach the Mystic" < reachzachatgooglesmailservice@dot.com> wrote:
>
> On 2/23/12 7:33 PM, Ellery Newcomer wrote:
>>>
>>> import std.stdio;
>>>
>>> mixin template helpMe()
>>> {
>>> writeln("Satisfying!");
>>> }
>>
>> does it do that if you replace the statement with a declaration?
>>
>> like this:
>>
>> mixin template helpMe()
>> {
>> int durrr = (writeln("Satisfying!"), 1);
>> }
>
>
> No, it doesn't. You're right. I guess I have a long way to go to learn
these things.
>
> Thank you. Even at my primitive level, though, I can see how awesome
these things could be once you know how to program them. Does any other language come close to D in terms of generics? I don't know, I'm just asking?
>
> Zach

Lisp macros. But that's not a fair comparison, Lisp's object system was built using their macros...

--
James Miller