www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin template FAIL

reply Zach the Mystic <reachzachatgooglesmailservice dot.com> writes:
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
Feb 21 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Feb 21 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
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
Feb 21 2012
prev sibling next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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.
Feb 21 2012
prev sibling parent reply Zach the Mystic <reachzachatgooglesmailservice dot.com> writes:
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
Feb 23 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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".
Feb 23 2012
prev sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
 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); }
Feb 23 2012
parent reply Zach the Mystic <reachzachatgooglesmailservice dot.com> writes:
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
Feb 24 2012
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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 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
Feb 24 2012
prev sibling parent James Miller <james aatch.net> writes:
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
Feb 24 2012