Bug 1986 – Mixin code from website doesn't compile
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-04-10T21:02:00Z
Last change time
2015-06-09T01:14:35Z
Keywords
diagnostic
Assigned to
nobody
Creator
bartosz
Comments
Comment #0 by bartosz — 2008-04-10T21:02:51Z
This example from http://www.digitalmars.com/d/2.0/mixin.html doesn’t compile:
template GenStruct(char[] Name, char[] M1) {
const char[] GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
}
mixin(GenStruct!("Foo", "bar"));
Comment #1 by gide — 2008-04-11T13:08:49Z
The corrected template doesn't compile, it looks like string /invariant(char)[]/ prevents compilation, changing the type to 'invariant char[]' and everything is ok.
Also the error message doesn't have a line number, so there is probably two bugs occuring.
main.d
------
template GenStruct(string Name, string M1) {
string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
}
mixin(GenStruct!("Foo", "bar"));
C:\>dmd main.d
attribute argument to mixin must be a string, not (GenStruct)
Comment #2 by gide — 2008-10-25T05:21:40Z
This is working in DMD v2.020. The line number is in error message, but the message is still confusing because GenStruct *is* declared as a string, it should be declared as an 'invariant char [] GenStruct = ...'.
main.d
------
template GenStruct(string Name, string M1) {
string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
}
mixin(GenStruct!("Foo", "bar"));
main.d(5): Error: argument to mixin must be a string, not (GenStruct)
Comment #3 by gide — 2008-12-16T10:08:55Z
Added keyword diagnostic and simplified example, should error message should say
"test.d(5): Error: argument to mixin must be a *invariant char[]* not (*invariant(char)[]*)"?
test.d
------
invariant char[] str1 = "int x1;"; // OK
mixin(str1); // OK
string str2 = "int x2;";
mixin(str2); // Fails, test.d(5): Error: argument to mixin must be a string, not (str2)
Comment #4 by k.hanazuki — 2011-10-25T08:21:35Z
*** Issue 6388 has been marked as a duplicate of this issue. ***
Comment #5 by andrej.mitrovich — 2012-12-01T16:04:09Z
> main.d(5): Error: argument to mixin must be a string, not (GenStruct)
is now:
> Error: variable GenStruct cannot be read at compile time
and
> Error: argument to mixin must be a string,
not (str2)
is:
> Error: variable str2 cannot be read at compile time