Sign in Join

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

Mailing lists / fpc-devel / 2024-10

virtual override with diff in default ?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Martin Frb

Is the below meant to be allowed?
It compiles, it also compiles if you have no default in the base, but a default in the sub class.

type
   TFoo = class
     procedure Bar (const Item: String; AnObject: TObject = nil); virtual;
   end;
   TFoo2 = class(TFoo)
     procedure Bar (const Item: String; AnObject: TObject); override;
   end;

It even works with both having a diff default.

   TFoo = class
     procedure Bar (const Item: String; AnObject: integer = 1); virtual;
   end;
   TFoo2 = class(TFoo)
     procedure Bar (const Item: String; AnObject: integer=2); override;
   end;

Reply by email

Sven Barth
to Martin Frb

Martin Frb via fpc-devel schrieb am Di.,
22. Okt. 2024, 23:28:

Show quoted text 22 lines
Is the below meant to be allowed? It compiles, it also compiles if you have no default in the base, but a default in the sub class. type TFoo = class procedure Bar (const Item: String; AnObject: TObject = nil); virtual; end; TFoo2 = class(TFoo) procedure Bar (const Item: String; AnObject: TObject); override; end; It even works with both having a diff default. TFoo = class procedure Bar (const Item: String; AnObject: integer = 1); virtual; end; TFoo2 = class(TFoo) procedure Bar (const Item: String; AnObject: integer=2); override; end;

A default value is not part of the signature. In the end the behavior will depend upon the concrete instance used: if youe variable is declared as TFoo then that default value will be used. If it's the first TFoo2 then the parameter is not optional and for the second TFoo2 the value passed in will be 2.

Regards,
Sven

Reply by email

n7800
to Martin Frb: Is the below meant to be allowed? It compiles, it also compiles if you have no default in…
Let me also share the results of tests for method pointers with different default values. The example is a bit convoluted, but it shows all possible combinations. It turns out that any component event definition always overrides the default values ​​from the class.
 
program Project1;
type
  TTest = class
    procedure test(a: string; b: string = 'class');
  end;
  TReq = procedure (a, b: string) of object;
  TOpt = procedure (a: string = 'def1'; b: string = 'def2') of object;
 
procedure TTest.test(a: string; b: string);
begin
  writeln(a, ', ', b);
end;
 
var
  lReq: TReq;
  lOpt: TOpt;
  lTest: TTest;
begin
  lTest := TTest.Create;
  lReq := @lTest.test;
  lOpt := @lTest.test;
 
  lReq('req1', 'req2');
  lOpt;
  TOpt(lReq);
  TReq(lOpt)('opt1', 'opt2');
end.
 
 
 
Show quoted text 27 lines
Среда, 23 октября 2024, 2:28 +05:00 от Martin Frb via fpc-devel :   Is the below meant to be allowed? It compiles, it also compiles if you have no default in the base, but a default in the sub class. type    TFoo = class      procedure Bar (const Item: String; AnObject: TObject = nil); virtual;    end;    TFoo2 = class(TFoo)      procedure Bar (const Item: String; AnObject: TObject); override;    end; It even works with both having a diff default.    TFoo = class      procedure Bar (const Item: String; AnObject: integer = 1); virtual;    end;    TFoo2 = class(TFoo)      procedure Bar (const Item: String; AnObject: integer=2); override;    end; _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

 

Reply by email