www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - UFCS overrides alias this

reply "Freddy" <Hexagonalstar64 gmail.com> writes:
----test.d
struct A{
	string b;
	alias b this;
}

struct MyRange{
	
}
char front(MyRange);
void popFront(ref MyRange);
bool empty(MyRange);

void test(A a){
	a.empty;
}
----
$ dmd -o- test
test.d(14): Error: function test.empty (MyRange) is not callable 
using argument types (A)
----
Is this intended behavior?
Apr 12 2015
parent reply "anonymous" <anonymous example.com> writes:
On Sunday, 12 April 2015 at 20:19:02 UTC, Freddy wrote:
 ----test.d
 struct A{
 	string b;
 	alias b this;
 }

 struct MyRange{
 	
 }
 char front(MyRange);
 void popFront(ref MyRange);
 bool empty(MyRange);

 void test(A a){
 	a.empty;
 }
 ----
 $ dmd -o- test
 test.d(14): Error: function test.empty (MyRange) is not 
 callable using argument types (A)
 ----
 Is this intended behavior?
string's empty is actually a function in std.array or std.range or something, called via UFCS. You don't import std's empty, so the call can't match even when the alias this is tried. Add the following, which brings std's empty into the overload set, and it works: static import std.range; alias empty = std.range.empty;
Apr 12 2015
parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
On Sunday, 12 April 2015 at 20:35:06 UTC, anonymous wrote:
 string's empty is actually a function in std.array or std.range 
 or something, called via UFCS. You don't import std's empty, so 
 the call can't match even when the alias this is tried.

 Add the following, which brings std's empty into the overload 
 set, and it works:

 static import std.range;
 alias empty = std.range.empty;
----test.d static import std.range; alias empty=std.range.empty; struct A{ string b; alias b this; } struct MyRange{ } char front(MyRange); void popFront(ref MyRange); bool empty(MyRange); void test(A a){ a.empty; } ---- $ dmd -o- test test.d(16): Error: overload alias 'empty' is not a variable ---- No idea what dmd is doing. However it works with ---- private auto empty(string s){ static import std.range; return std.range.empty(s); } ----
Apr 12 2015
parent "anonymous" <anonymous example.com> writes:
On Sunday, 12 April 2015 at 20:48:57 UTC, Freddy wrote:
 ----test.d
 static import std.range;
 alias empty=std.range.empty;
 struct A{
 	string b;
 	alias b this;
 }

 struct MyRange{
 	
 }
 char front(MyRange);
 void popFront(ref MyRange);
 bool empty(MyRange);

 void test(A a){
 	a.empty;
 }
 ----
 $ dmd -o- test
 test.d(16): Error: overload alias 'empty' is not a variable
 ----
 No idea what dmd is doing.
Looks like order matters. This works: bool empty(MyRange); alias empty = std.range.empty; This doesn't: alias empty = std.range.empty; bool empty(MyRange); I don't know if there's a reason for that or if dmd is just being silly.
Apr 12 2015