Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-02

two questions about generics

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

Hi all,

I have two questions about generics. I am writing a two-dimensional table class. Say:

type
  generic TTable<TValue> = class
  ... ...
  public
    constructor Create(ADefaultValue: TValue);
  end;

My questions are:

1) if I am not writing a generic class I can define default value for the constructor, i.e.

constructor Create(ADefaultValue: Double = 0);

for generic classes, how can I define a default value?

2) if I cannot define a default value, I hope to do so while specializing it. i.e.

type
  TNumericTable = specialize TTable<Double>
  public
    constructor Create(ADefaultValue: Double = 0); override;
  end;

That is, can I "inherit" a generic class while specializing it?

Thanks,
Shannon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130222/89eafebe/attachment.html>

Reply by email

Xiangrong Fang
to Sven Barth: You can use the new intrinsic Default(TypeName) which is available in FPC 2.7.1. Your…

Thanks Sven. In the case I use Default() does it mean it always has fixed defaults with specific types, e.g. 0 for integer '' for string. Or I can set my own default value?

2013/2/22 Sven Barth

Show quoted text 84 lines
On 22.02.2013 04:48, Xiangrong Fang wrote: > Hi all, > > I have two questions about generics. I am writing a two-dimensional > table class. Say: > > type > generic TTable<TValue> = class > ... ... > public > constructor Create(ADefaultValue: TValue); > end; > > My questions are: > > 1) if I am not writing a generic class I can define default value for > the constructor, i.e. > > constructor Create(ADefaultValue: Double = 0); > > for generic classes, how can I define a default value? > You can use the new intrinsic Default(TypeName) which is available in FPC 2.7.1. Your declaration would look like this: === snippet begin === constructor Create(ADefaultValue: TValue = Default(TValue)); === snippet end === You need to pay attention though with what you specialize TTable. You can't use a record for example, because records can not have default values in parameters (this has nothing to do with generics or the Default() intrinsic). Classes and interfaces are ok they as are all primitive types. As a sidenote: Default() can not only be used in constructor parameter lists, but also for normal functions and in normal code. Using it in const sections or var sections does not yet work in every case (primitive types work, classes, interfaces, records and objects do not). > 2) if I cannot define a default value, I hope to do so while > specializing it. i.e. > > type > TNumericTable = specialize TTable<Double> > public > constructor Create(ADefaultValue: Double = 0); override; > end; > > That is, can I "inherit" a generic class while specializing it? > You would need to write it this way: === code begin === type TNumericTable = class(specialize TTable<Double>) public constructor Create(ADefaultValue: Double = 0); end; constructor TNumericTable.Create(**ADefaultValue: Double); begin inherited Create(ADefaultValue); end; === code end === Regards, Sven -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130222/ed5aa601/attachment.html>

Reply by email

Sven Barth
to Xiangrong Fang: I have two questions about generics. I am writing a two-dimensional table class. Say:…
Xiangrong Fang wrote 20 lines
Hi all, I have two questions about generics. I am writing a two-dimensional table class. Say: type generic TTable<TValue> = class ... ... public constructor Create(ADefaultValue: TValue); end; My questions are: 1) if I am not writing a generic class I can define default value for the constructor, i.e. constructor Create(ADefaultValue: Double = 0); for generic classes, how can I define a default value?

You can use the new intrinsic Default(TypeName) which is available in FPC 2.7.1. Your declaration would look like this:

=== snippet begin ===

constructor Create(ADefaultValue: TValue = Default(TValue));

=== snippet end ===

You need to pay attention though with what you specialize TTable. You can't use a record for example, because records can not have default values in parameters (this has nothing to do with generics or the Default() intrinsic). Classes and interfaces are ok they as are all primitive types.

As a sidenote: Default() can not only be used in constructor parameter lists, but also for normal functions and in normal code. Using it in const sections or var sections does not yet work in every case (primitive types work, classes, interfaces, records and objects do not).

Show quoted text 11 lines
2) if I cannot define a default value, I hope to do so while specializing it. i.e. type TNumericTable = specialize TTable<Double> public constructor Create(ADefaultValue: Double = 0); override; end; That is, can I "inherit" a generic class while specializing it?

You would need to write it this way:

=== code begin ===

type
   TNumericTable = class(specialize TTable<Double>)
   public
     constructor Create(ADefaultValue: Double = 0);
   end;
constructor TNumericTable.Create(ADefaultValue: Double);
begin
   inherited Create(ADefaultValue);
end;

=== code end ===

Regards,
Sven

Reply by email

Sven Barth
to Xiangrong Fang: Thanks Sven. In the case I use Default() does it mean it always has fixed defaults with…
Xiangrong Fang wrote
Thanks Sven. In the case I use Default() does it mean it always has fixed defaults with specific types, e.g. 0 for integer '' for string. Or I can set my own default value?

It is a fixed default value. Simply spoken it's always zero. Even in cases like the following:

=== example begin ===

type
   TTest = record
     case Boolean of
       True: (a: 5..10);
       False: (b: -10..-5);
   end;
var
   t: TTest;
begin
   t := Default(TTest);
   Writeln(t.a); // 0
   Writeln(t.b); // 0
end.

=== example end ===

Regards,
Sven

Reply by email