inout delegate
Manu via Digitalmars-d
digitalmars-d at puremagic.com
Sun Oct 2 04:27:09 PDT 2016
And what's the logic behind this:
class Test
{
int f() { return 10; }
static assert(is(typeof(&typeof(this).f) == int function())); // huh?
static void t()
{
// as expected:
f(); // error : need 'this' for 'f' of type 'int()'
// calling a method without a context pointer...
auto x = &f;
x(); // call the method with no context pointer, how is this okay?
pragma(msg, typeof(x)); // prints: int function()
}
}
If f() were supplied an argument, the function would receive the first
arg as the context pointer, and the rest would be out by 1! O_o
I don't see how this code can be acceptable?
If such a thing (getting a static function pointer from a delegate)
were to be supported, surely: is(typeof(&Test.f) == int
function(Test)), though this is only valid for some (most) abi's.
On 2 October 2016 at 19:55, Manu <turkeyman at gmail.com> wrote:
> Can someone explain this to me?
>
> class Test
> {
> inout(int) f() inout { return 10; }
>
> void t()
> {
> f(); // calls fine with mutable 'this'
> auto d = &this.f; // error : inout method Test.f is not callable
> using a mutable this
> d();
> }
> }
>
> That error message seems very unhelpful, and it's not true. Of course
> an inout method is callable with a mutable 'this'...
>
> I suspect that the problem is the type for the delegate; "inout(int)
> delegate()" doesn't leave anything for the type system to resolve the
> inout with.
> I guess the expectation is that this delegate has it's inout-ness
> resolved when you capture the delegate:
> is(typeof(&this.f) == int delegate())
> Or if 'this' were const:
> is(typeof(&this.f) == const(int) delegate())
>
> But I get this unhelpful error instead.
>
> What's the story here?
More information about the Digitalmars-d
mailing list