Is @property implementable?

Jonathan M Davis jmdavisProg at gmx.com
Wed Mar 2 17:56:41 PST 2011


On Wednesday, March 02, 2011 16:57:42 Michel Fortin wrote:
> On 2011-03-02 17:14:30 -0500, Jonathan M Davis <jmdavisProg at gmx.com> said:
> > I would expect the compiler to be smart enough to combine the fact that
> > you can call functions on arrays as if they were member functions and
> > @property to allow
> > you to deal with any property as if it were a member of the array. So,
> > the fact that the property is declared external to the array should be
> > irrelevant. It should work just the same as any property would on a
> > user-defined type.
> 
> The basic problem is that if you declare this function:
> 
> 	@property int front(int[] array);
> 
> you can interpret this declaration in two ways. The standard way, where
> the function behaves as a setter for the scope it belongs in:
> 
> 	front = array;
> 
> and the "array member" way, where the function behaves as a getter for
> the property of an array:
> 
> 	array.front;
> 
> How is the compiler supposed to determine which one of the two you
> meant (setter or getter) when you declared the function?

I would argue that properties make no sense unless they have a "this" parameter 
to work. In the case of arrays, the first parameter is effectively the "this" 
parameter - it just isn't invisible like it is with a class or struct. I don't 
think that a property which isn't called on something makes any sense at all. 
So,

front = array;

makes no sense at all and wouldn't even be considered by the compiler. In the 
case of a struct or class, a getter property is a function which takes nothing 
(except the invisible this parameter) and returns a value, whereas a setter 
property is a function which returns nothing but takes only the value to set 
(and the invisible this parameter) - or a property function is both a getter and 
a setter but takes nothing (except the invisible this parameter) and returns a 
ref.

In the case of arrays or uniform function call syntax (if it ever gets 
implemented), a getter property is a function at module scope which takes only 
an array (or whatever the type is if it's using UFCS) and returns a value, 
whereas a setter property is a function at module scope which returns nothing 
and takes only the array (or whatever type is if it's using UFCS) and the value 
to set - or a property function is both a getter and a setter but takes only the 
array (or whatever the type is if it's using UFCS) and returns a ref.

I don't see any ambiguity here. A property makes no sense unless it's a property 
_of_ something.

- Jonathan M Davis


More information about the Digitalmars-d mailing list