Uniform Function Call syntax for properties

Denis Koroskin 2korden at gmail.com
Fri Oct 8 06:30:59 PDT 2010


On Fri, 08 Oct 2010 17:26:41 +0400, Stanislav Blinov <blinov at loniir.ru>  
wrote:

>   08.10.2010 16:55, Steven Schveighoffer пишет:
>> Someone was asking about UFC syntax for properties on d.learn, and I  
>> realized, we have a huge ambiguity here.
>>
>> Given a function:
>>
>> @property int foo(int x)
>>
>> Is this a global setter or a getter on an int?
>>
>> i.e.
>>
>> int y = foo = 3;
>>
>> or
>>
>> int y = (3).foo;
>>
>> ???
>>
>> I know arrays have an issue with property setters, see a bug report I  
>> filed a while back:
>>
>> http://d.puremagic.com/issues/show_bug.cgi?id=3857
>>
>> But that seems like a totally unambiguous case (a global property  
>> function with two parameters is obviously a setter on the first  
>> parameter in UFC).
>>
>> The getter seems much more problematic.  Does anyone have a solution  
>> besides adding more syntax?  Should we disallow global properties to  
>> avoid ambiguity?
>>
> What if we make @properties have first parameter as ref/ref const if  
> it's meant to be part of UFC? This way we can keep module-scope  
> properties and easily make UFC properties for existing types:
>
> @property int foo(int x) { /*...*/ } // module-scope setter
> @property int foo() { /*...*/ } // module-scope getter
> @property int foo(ref int x, int v) { /*...*/ } // UFC 'setter' for int
> @property int foo(ref int x) { /*...*/ } // or int foo(ref const int x)  
> - UFS 'getter' for int
>
> There's a caveat, though, as having ref/ref const would disallow doing  
> thigs like 3.foo() (which makes perfect sense for 'setters', but not for  
> getters.

C# uses 'this' keyword for that purpose:

@property int set(this int x, int y)
{
     x = y;
}


@property int get(this const(int) x)
{
     return x;
}

int a = 1;
a.set(42); // a is 42 now
3.set(42); // fails to compile, 3 is of type const(int)


More information about the Digitalmars-d mailing list