Uniform call syntax for implicit this.

Steven Schveighoffer schveiguy at yahoo.com
Thu Feb 3 11:43:53 PST 2011


On Thu, 03 Feb 2011 14:15:11 -0500, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2011-02-03 13:42:30 -0500, Jonathan M Davis <jmdavisProg at gmx.com>  
> said:
>
>> On Thursday, February 03, 2011 09:54:44 Michel Fortin wrote:
>>> On 2011-02-03 12:43:12 -0500, Daniel Gibson <metalcaedes at gmail.com>  
>>> said:
>>>> Am 03.02.2011 15:57, schrieb Michel Fortin:
>>>>> On 2011-02-02 23:48:15 -0500, %u <dflgkd at sgjds.com> said:
>>>>>> When implemented, will uniform call syntax work for the "this"
>>>>>> object even if not specified?
>>>>>>  For example, will foo() get called in the following example?
>>>>>>  void foo(A a, int b) {}
>>>>>>  class A {
>>>>>> void test() {
>>>>>> this.foo(10);
>>>>>> foo(10);
>>>>>> }
>>>>>> }
>>>>>  I think it should work.
>>>>  I think foo(10) should *not* be equivalent to foo(this, 10).
>>>  Personally, I'm not sure whether the uniform call syntax will be this
>>> much useful or not, but if it gets implemented I think foo(10) should
>>> be equivalent to foo(this, 10) in the case above. That said, it should
>>> not be ambiguous: if there is a member function foo and a global
>>> function foo and both matches the call, it's ambiguous and it should be
>>> an error.
>>>  Can this work in practice? We probably won't know until we have an
>>> implementation to play with.
>>  Except that if you have both a member function foo and a free function  
>> foo, how
>> can you tell the compiler which to use?
>
> Indeed, that's a problem. The solution is to sidestep the problem. :-)
>
> We just have to disallow declaring a module-level function and a  
> class-level function with the same name and the same parameter types  
> (including 'this' in the parameters).
>
> 	void foo(A a, int i);
>
> 	class A {
> 		void foo(int i); // error, same as foo(A, int)
> 	}
>
> If the module-level function is in a different module, then you can use  
> the module name to disambiguate if necessary. If they're in the same  
> module, the compiler catches the error during semantic analysis of the  
> module.
>
> I'm not too sure how disruptive this change would be to existing code.  
> I'm under the impression that this situation is rare, but I can't say  
> for sure.


The more I think about it (and read discussions like this), the more I  
think that uniform call syntax should only be allowed for types which  
cannot declare member functions (arrays, primitives, enums, etc.).   
Otherwise, we introduce new ambiguities that serve little purpose except  
annoyance.

If I have a function foo(A) and A has a member foo(), then foo(myA) should  
call the free function, myA.foo() should call the member.  If this doesn't  
continue to be correct, we have taken a step backwards.

-Steve


More information about the Digitalmars-d mailing list