Lazarus

Free Pascal => General => Topic started by: avk on July 14, 2020, 10:34:09 am

Title: Default parameter value in a generic procedure/function
Post by: avk on July 14, 2020, 10:34:09 am
I suppose this simple example would have to compile successfully?
Code: Pascal  [Select][+][-]
  1. program def_param;
  2.  
  3. {$mode delphi}
  4.  
  5. function Test<T>(a, b: T; c: T = T(1)): T;
  6. begin
  7.   Result := a + b + c;
  8. end;
  9.  
  10. begin
  11.   WriteLn(Test<Single>(2, 3));
  12. end.
  13.  
At least it compiles in objfpc mode.
FPC-3.2.1 r45770 x86_64-linux.
Title: Re: Default parameter value in a generic procedure/function
Post by: PascalDragon on July 14, 2020, 01:59:19 pm
It doesn't compile without generics either:

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. function Foobar(a: Single; b: Single = Single(1)): Single;
  4. begin
  5.  
  6. end;  
  7.  
  8. begin
  9. end.

So this could either be an incompatibility with Delphi or it's simply that Delphi does not allow that.
Title: Re: Default parameter value in a generic procedure/function
Post by: avk on July 14, 2020, 03:25:21 pm
IIRC Delphi does not support generic procedures/functions, but your example should compile.
Also, the current trunk successfully compiles my example in both modes.
Title: Re: Default parameter value in a generic procedure/function
Post by: PascalDragon on July 15, 2020, 09:52:16 am
IIRC Delphi does not support generic procedures/functions, but your example should compile.

But the example can easily be converted to generic methods for testing with Delphi.

Also, the current trunk successfully compiles my example in both modes.

Then probably something slipped by when merging to 3.2...  %)
Title: Re: Default parameter value in a generic procedure/function
Post by: PascalDragon on July 15, 2020, 11:31:30 pm
IIRC Delphi does not support generic procedures/functions, but your example should compile.

But the example can easily be converted to generic methods for testing with Delphi.

So, Delphi does not allow generic default parameters... That said FPC's generics are a bit less strict in that regard and I'll simple keep default parameters allowed in all modes that support default parameters. ;D
Title: Re: Default parameter value in a generic procedure/function
Post by: avk on July 16, 2020, 03:06:43 pm
So what will be the verdict?
Leave it as is or report a bug against 3.2.1?
Title: Re: Default parameter value in a generic procedure/function
Post by: PascalDragon on July 16, 2020, 03:42:03 pm
Well, you can report it, but mention that it's fixed in trunk and that it's merely about finding the correct revision(s) that fixed this so that they can be merged to 3.2.
Title: Re: Default parameter value in a generic procedure/function
Post by: avk on July 17, 2020, 10:44:08 am
Thank you, done.
TinyPortal © 2005-2018