D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13079 - Need 'this' to access member - function literal
Summary: Need 'this' to access member - function literal
Status: RESOLVED DUPLICATE of issue 11545
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-08 19:04 UTC by Daniel Čejchan
Modified: 2014-07-15 06:48 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Daniel Čejchan 2014-07-08 19:04:14 UTC
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 hsteoh 2014-07-14 14:57:36 UTC
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 Daniel Čejchan 2014-07-14 15:57:01 UTC
(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 hsteoh 2014-07-14 18:29:13 UTC
Sorry, it should be:
----
class Test {
    mixin({ return ""; }());
}
----
Comment 4 Daniel Čejchan 2014-07-14 18:39:08 UTC
(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 hsteoh 2014-07-14 18:44:38 UTC
Hmm. That one works in git HEAD. Is it a bug that was recently fixed?
Comment 6 hsteoh 2014-07-14 18:46:00 UTC
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 Daniel Čejchan 2014-07-14 20:36:04 UTC
(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 hsteoh 2014-07-14 22:24:36 UTC
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 Kenji Hara 2014-07-15 05:39:14 UTC
(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 Daniel Čejchan 2014-07-15 06:48:06 UTC
(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