Jump to content
Tommi Prami

Making method with default encoding

Recommended Posts

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 by Tommi Prami

Share this post


Link to post
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 by David Heffernan
  • Like 1
  • Sad 1

Share this post


Link to post

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;

 

  • Like 1

Share this post


Link to post
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. 

  • Like 1

Share this post


Link to post

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

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
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
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. 

  • Like 1

Share this post


Link to post

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 by Remy Lebeau

Share this post


Link to post

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×