[fpc-devel] [] property overloads

Michael Van Canneyt michael at freepascal.org
Thu Jun 20 21:58:58 CEST 2019



On Thu, 20 Jun 2019, Ryan Joseph wrote:

>
>
>> On Jun 20, 2019, at 2:17 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>> 
>> IMHO this goes contrary to what the word 'default' means. There can be only
>> 1 default.
>
> I think the name of “default” was probably a mistake and it should have been “nameless” or something similar. Not sure why it was chosen but it’s going to be giving us problems.
>
>> 
>> That said, allowing overloading of array properties based on index type has been on my wish list since a long time.
>> 
>> So
>>  property Values[index: integer]: TValue read GetValue;
>>  property Values[index: string]: TValue read GetValue; would be a nice addition.
>> 
>> Since the Values array is then uniquely named, 'Default' makes sense again.
>
> The values array is uniquely named? Looks the same to me.

The point is that there is in my example only 'Values' which is a unique name. 
Making that 'Default' just means that the 'Values' keyword can be left out.
making the 'Values' array the single default.

> Properties don’t support duplicate identifiers though and the real issue is 
> being able to do c[0] or c[‘key’] without the property name.

I am aware of what you're trying to do.

> It feels to me like we’re being held back by the choice of the word default. How can we get around that? Seems like a silly thing to be getting in our way.

Allowing multiple index types for array properties should solve the matter nicely, 
even combined with "default". Since there are always getters and setters for such an array
property, the normal overloading rules apply.

So you can do
   C.Values[0];
   C.Values['key'];
or
   C[0];
   C['key'] ;

I always felt it silly that I must do

MyStrings.Items[0]
and
MyStrings.Values['Name']

when the compiler could perfectly see what overload to use when I type

MyStrings.items[0] 
MyStrings.items['Name']
And, by extension
MyStrings[0] 
MyStrings['Name']

Still you must be careful, because what is meant by
MyStrings['0'] 
... is it a type error or is actually the name meant ?

But maybe the compiler people can see a reason why this should not be allowed.

Michael.


More information about the fpc-devel mailing list