[fpc-devel] [] property overloads
Marcus Marques da Rocha
mmr at prognum.com.br
Thu Jun 20 20:08:38 CEST 2019
It is possible to do that using variant type. I have done it as below:
property DocumentElementChildNodes[Index1: Variant] : TPXMLNode read
getChildNode; default;
function TPXMLNode.getChildNode(Index1 : Variant): TPXMLNode;
begin
Result := nil;
if assigned(Self) and assigned(FChildNodes) then
if (varType(Index1) = varString) or (varType(Index1) = varUString) then
begin
if (IndexOf(Index1) = -1) then
if FAutoCreateChildren then
result := TPXmlNode(self.addchild(Index1))
else
result := nil
else
Result := TPXMLNode( FChildNodes.objects[IndexOf(Index1)]);
end
else
Result := TPXMLNode(FChildNodes.objects[Index1]);
end;
Regards
Marcus Rocha.
Em 20/06/2019 14:57, Ryan Joseph escreveu:
> 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
>
> Regards,
> Ryan Joseph
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
More information about the fpc-devel
mailing list