Uniform Function Call syntax for properties

Stanislav Blinov blinov at loniir.ru
Fri Oct 8 06:26:41 PDT 2010


  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.


More information about the Digitalmars-d mailing list