Bug 13079 – Need 'this' to access member - function literal
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2014-07-08T19:04:00Z
Last change time
2014-07-15T06:48:06Z
Assigned to
nobody
Creator
czdanol
Comments
Comment #0 by czdanol — 2014-07-08T19:04:14Z
Hey, this code can't be compiled, although it should:
class Test {
mixin( function string() {
return "";
}() );
}
void main() {
}
The error is: Error: function ...Test() need 'this' to access member __funcliteral2
Comment #1 by hsteoh — 2014-07-14T14:57:36Z
Are you sure that's valid syntax? Don't you mean this:
----
class Test {
mixin(string func() {
return "";
});
}
----
The diagnostic should be improved, though. The current error message doesn't make sense.
Comment #2 by czdanol — 2014-07-14T15:57:01Z
(In reply to hsteoh from comment #1)
> Are you sure that's valid syntax? Don't you mean this:
> ----
> class Test {
> mixin(string func() {
> return "";
> });
> }
> ----
> The diagnostic should be improved, though. The current error message doesn't
> make sense.
That one can't be compiled either.
Comment #3 by hsteoh — 2014-07-14T18:29:13Z
Sorry, it should be:
----
class Test {
mixin({ return ""; }());
}
----
Comment #4 by czdanol — 2014-07-14T18:39:08Z
(In reply to hsteoh from comment #3)
> Sorry, it should be:
> ----
> class Test {
> mixin({ return ""; }());
> }
> ----
Neither that one can be compiled :P
http://dpaste.dzfl.pl/6fa47fdf9f35
Comment #5 by hsteoh — 2014-07-14T18:44:38Z
Hmm. That one works in git HEAD. Is it a bug that was recently fixed?
Comment #6 by hsteoh — 2014-07-14T18:46:00Z
I'm also curious why you want this odd construction to work. I assume you have a non-trivial use case in mind? Because otherwise, you could just write mixin(""); directly instead of going a roundabout way using function literals.
Comment #7 by czdanol — 2014-07-14T20:36:04Z
(In reply to hsteoh from comment #6)
> I'm also curious why you want this odd construction to work. I assume you
> have a non-trivial use case in mind? Because otherwise, you could just write
> mixin(""); directly instead of going a roundabout way using function
> literals.
http://pastebin.com/r1nwiQUK
Comment #8 by hsteoh — 2014-07-14T22:24:36Z
Ahh, I see. So you're basically using mixin as a macro system for generating code inside a single class. Makes sense.
The following code works for me (dmd git HEAD):
----
import std.string;
class C {
mixin({
string code="";
foreach (i; 0..10) {
code ~= format("int x%d;\n", i);
}
return code;
}());
}
void main() {
foreach (memb; __traits(allMembers, C)) {
static if (is(typeof(__traits(getMember, C.init, memb)) T))
{
pragma(msg, memb);
}
}
}
----
So obviously, it's possible to use a code block to generate declarations inside the class, at least on dmd git HEAD. You should be able to adapt this code to your needs. (The code in main() is just to prove that those declarations actually got inserted, you can ignore it.)
Which version of dmd are you using?
Comment #9 by k.hara.pg — 2014-07-15T05:39:14Z
(In reply to hsteoh from comment #5)
> Hmm. That one works in git HEAD. Is it a bug that was recently fixed?
Dup of issue 11545, and it will be properly fixed in 2.066 release.
*** This issue has been marked as a duplicate of issue 11545 ***
Comment #10 by czdanol — 2014-07-15T06:48:06Z
(In reply to hsteoh from comment #8)
> Ahh, I see. So you're basically using mixin as a macro system for generating
> code inside a single class. Makes sense.
>
> The following code works for me (dmd git HEAD):
> ...
> So obviously, it's possible to use a code block to generate declarations
> inside the class, at least on dmd git HEAD. You should be able to adapt this
> code to your needs. (The code in main() is just to prove that those
> declarations actually got inserted, you can ignore it.)
>
> Which version of dmd are you using?
http://dpaste.dzfl.pl/2f156080d7e3 :P
DPaste has 2.065