Tommi Prami 130 Posted April 29, 2020 (edited) as far as I know I cant do something like (at least compiler did not like that) procedure Save(const AData:TStringList; const AEncoding: TEncoding = TEncoding.UTF8); begin AData.SaveToFile(FileName, AEncoding); end; Is there any standard way to pass encoding default (For certain case). Quick look did not give any results (RTL code adn Google) Edited April 29, 2020 by Tommi Prami Share this post Link to post
David Heffernan 2345 Posted April 29, 2020 (edited) 39 minutes ago, Lars Fosdal said: TEncoding.Default ? Default values need to be true constants. This is not. You are confusing default encoding with default parameter value. Edited April 29, 2020 by David Heffernan 1 1 Share this post Link to post
Uwe Raabe 2057 Posted April 29, 2020 I guess you have to use overload methods. function Format(const Format: string; const Args: array of const): string; overload; function Format(const Format: string; const Args: array of const; const AFormatSettings: TFormatSettings): string; overload; 1 Share this post Link to post
David Heffernan 2345 Posted April 29, 2020 38 minutes ago, Tommi Prami said: Is there any standard way to pass encoding default You can't declare an encoding instance to be a default parameter value because it is not a constant, never mind a true constant. Hence the compiler error. So you have to resort to overloaded methods. Declare one method that accepts an encoding parameter. And another that does not. From that second overload call the first passing your chosen default encoding. 1 Share this post Link to post
Attila Kovacs 629 Posted April 29, 2020 for TEncoding you can set it nil as default and use your favorite one (as default) if the param is nil 1 Share this post Link to post
Lars Fosdal 1792 Posted April 29, 2020 A helper type / class could be an alternative. TEncodingType = (encDefault, encUTF7, encUTF8, encUnicode, encBigEndianUnicode, encMBCS, encExpandThisTypeAsNeeded); TEncodingHelper = record helper for TEncodingType function Encoding: TEncoding; overload; // which calls the method below with Self function Encoding(const aEncodingType: TEncodingType): TEncoding; overload; // which look up the relevant TEncoding constants. end; Share this post Link to post
Tommi Prami 130 Posted April 29, 2020 I clarify little. what mean by Default, not the Delphi default but MY DEFAULT what it ever would be. in current context. I also was thinking of using enumerated type or so, but it is not that official, Delphi defined way. Seems that this has not been thought at all while implementing this. There are ways around this, but seems no way to define some (random) defaulöt method parameter. -Tee- Share this post Link to post
Lars Fosdal 1792 Posted April 29, 2020 TEncodingType = (encDefault, encUTF7, encUTF8, encUnicode, encBigEndianUnicode, encMBCS, encExpandThisTypeAsNeeded); TEncodingHelper = record helper for TEncodingType private class var FMyDefault: TEncodingType; public function Encoding: TEncoding; overload; // which calls the method below with Self function Encoding(const aEncodingType: TEncodingType): TEncoding; overload; // which look up the relevant TEncoding constants. class property FMyDefault: TEncodingType read FMyDefault write FMyDefault; end; Share this post Link to post
David Heffernan 2345 Posted April 29, 2020 30 minutes ago, Tommi Prami said: I clarify little. what mean by Default, not the Delphi default but MY DEFAULT what it ever would be. in current context. I also was thinking of using enumerated type or so, but it is not that official, Delphi defined way. Seems that this has not been thought at all while implementing this. There are ways around this, but seems no way to define some (random) defaulöt method parameter. -Tee- This provides no clarification to me. Now I think I have no idea what you are asking. 1 Share this post Link to post
Remy Lebeau 1394 Posted April 29, 2020 (edited) Being a class type, you really can't specify a specific TEncoding value as a default parameter value. So you have to either: Use an overload (that is what Delphi's RTL does): procedure Save(const AData: TStringList); overload; procedure Save(const AData: TStringList; const AEncoding: TEncoding); overload; ... procedure Save(const AData: TStringList); begin Save(AData, TEncoding.UTF8); end; procedure Save(const AData: TStringList; const AEncoding: TEncoding); begin AData.SaveToFile(FileName, AEncoding); end; Or else use nil for the default value: procedure Save(const AData: TStringList; AEncoding: TEncoding = nil); ... procedure Save(const AData: TStringList; AEncoding: TEncoding); begin if AEncoding = nil then AEncoding := TEncoding.UTF8; AData.SaveToFile(FileName, AEncoding); end; Edited April 29, 2020 by Remy Lebeau Share this post Link to post
Fr0sT.Brutal 900 Posted May 4, 2020 I'd prefer using nil as a sign of taking some previously configured encoding. Or, if you dare, you could use code page numbers. Share this post Link to post