[fpc-devel] [] property overloads

Michael Van Canneyt michael at freepascal.org
Thu Jun 20 20:17:15 CEST 2019



On Thu, 20 Jun 2019, Ryan Joseph wrote:

> I just had some plans for a cool JSON class thwarted because apparently []
> properties don’t allow overloading of the parameters.  Can we allow
> multiple default properties as long as their parameters are different?  I
> know there’s not overloading of property names but at least the parameters
> could be overloadable so the correct property is chosen depending on what
> the args are.
>
> type
>  TMyClass = class
>    function GetValueWithInt(index: integer): TValue;
>    function GetValueWithString(index: string): TValue;
>    property IndexI[index: integer]: TValue read GetValueWithInt; default;
>    property IndexS[index: string]: TValue read GetValueWithString; default;
>  end;
>
> 
> o := c[index]; // IndexI wins
> o := c[‘key’]; // IndexS wins

IMHO this goes contrary to what the word 'default' means. There can be only
1 default.

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.

Michael.


More information about the fpc-devel mailing list