Thread overview
[Issue 3452] New: Can't alias member functions such that the object name is implicitly stored in the alias
Oct 29, 2009
David Simcha
Feb 08, 2013
Andrej Mitrovic
Aug 06, 2013
Dicebot
Aug 06, 2013
Maxim Fomin
Aug 07, 2013
Dicebot
October 29, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3452

           Summary: Can't alias member functions such that the object name
                    is implicitly stored in the alias
           Product: D
           Version: 2.035
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: dsimcha@yahoo.com


--- Comment #0 from David Simcha <dsimcha@yahoo.com> 2009-10-29 14:00:43 PDT ---
struct Foo {
    void bar() {}
    void baz() {}
}

void doStuff(Foo foo) {
    alias foo.bar fun;
    fun();  //  Error: need 'this' to access member bar
}

I can't think of any reason why this shouldn't work.  foo.bar is a compile-time
symbol for the member function Foo.bar() on the instance foo.  Once I alias
foo.bar to fun, calling fun() should be equivalent to calling foo.bar().

Even if there's some language legalese reason why this shouldn't work according to the spec, it's still a reasonable enhancement request.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3452


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samukha@voliacable.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-08 15:36:42 PST ---
*** Issue 7828 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3452


Dicebot <public@dicebot.lv> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |public@dicebot.lv


--- Comment #2 from Dicebot <public@dicebot.lv> 2013-08-06 09:46:54 PDT ---
Another application is template alias parameter:

string boo(alias T)()
{
    return T.stringof;
}

struct A
{
    int field;
}

pragma(msg, boo!(A.field)());

currently this fails with "Error: need 'this' for 'boo' of type 'nothrow @safe string()'" which does not make sense because compile-time usage of symbol "A.field" does not require "this" pointer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3452


Maxim Fomin <maxim@maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim@maxim-fomin.ru


--- Comment #3 from Maxim Fomin <maxim@maxim-fomin.ru> 2013-08-06 10:12:00 PDT ---
(In reply to comment #0)
> struct Foo {
>     void bar() {}
>     void baz() {}
> }
> 
> void doStuff(Foo foo) {
>     alias foo.bar fun;
>     fun();  //  Error: need 'this' to access member bar
> }
> 

Actually error message is reasonable for the same reason as Foo.bar requires this pointer. The root of the issue is that alias does not capture local variable in this context - it captures only type name. The code above is essentially alias Foo.bar fun and since bar() is nonstatic the code doesn't compile.

> I can't think of any reason why this shouldn't work.  foo.bar is a compile-time
> symbol for the member function Foo.bar() on the instance foo.  Once I alias
> foo.bar to fun, calling fun() should be equivalent to calling foo.bar().

Because of underspecification many view things different to what compiler does.

> Even if there's some language legalese reason why this shouldn't work according to the spec, it's still a reasonable enhancement request.

Yes, but this isn't a minor enhancement.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3452



--- Comment #4 from Dicebot <public@dicebot.lv> 2013-08-07 06:27:12 PDT ---
(In reply to comment #3)
> The root of the issue is that alias does not capture local
> variable in this context - it captures only type name. The code above is
> essentially alias Foo.bar fun and since bar() is nonstatic the code doesn't
> compile.

There is a big issue with `alias` specification because of no clear definition what is captured. Current documentation simply describes behavior of reference implementation in various cases which is rather inconsistent on its own (as far as I am aware, there is no even common alias handling in dmd).

Now my understanding of the `alias` concept is simple - it should capture symbols, be it type symbol or variable symbol or anonymous lambda literal symbol.

But I think you are right - cleaning this is worth separate DIP, plenty of corner cases will arise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------