D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11150 - Symbol alias of member function impossible to be called
Summary: Symbol alias of member function impossible to be called
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-30 15:26 UTC by Andrej Mitrovic
Modified: 2022-07-04 16:58 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2013-09-30 15:26:34 UTC
-----
struct S
{
    T getData(T)() { return T.init; }
}

alias getInt = S.getData!int;

void main()
{
    auto s = S();
    int x = getInt();    // error: need 'this' (expected error)
    int y = s.getInt();  // error: not callable with S (hmm...)
}
-----

Currently it's not possible to easily create an alias of an aggregate template, forcing the user to always explicitly instantiate such a template at the call site.

The workaround is to use a helper UFCS template that forwards to an internal template:

-----
// workaround
T getData(T)(S s) { return s.getDataImpl!T(); }

struct S
{
    T getDataImpl(T)() { return T.init; }
}

alias getInt = getData!int;

void main()
{
    auto s = S();
    int x = s.getInt();  // ok
    // int y = getInt(); // errors, as it should
}
-----

But it would be much simpler if we could use the alias syntax for this.

Since this might be a big enhancement it could use a forum discussion and maybe a DIP.
Comment 1 monarchdodra 2013-10-01 00:33:59 UTC
(In reply to comment #0)
> Currently it's not possible to easily create an alias of an aggregate template,

I'm not sure this has anything to do with templates?

//----
struct S
{
   int getData() { return int.init; }
}

alias getInt = S.getData;

void main()
{
   auto s = S();
   int x = getInt();    // error: need 'this' (expected error)
   int y = s.getInt();  // error: not callable with S (hmm...)
}
//----

Same result.
Comment 2 Andrej Mitrovic 2013-10-01 04:06:44 UTC
(In reply to comment #1)
> (In reply to comment #0)
> > Currently it's not possible to easily create an alias of an aggregate template,
> 
> I'm not sure this has anything to do with templates?

Maybe a more generalized term like external member aliasing would be the actual feature. Then template support becomes automatic.

If it were allowed then API writers wouldn't have to worry too much about adding convenience member aliases, since the users can do it themselves.
Comment 3 Andrej Mitrovic 2022-07-04 16:58:46 UTC
This is an invalid issue. Understanding how member function calls work makes this clear.