Lazarus

Free Pascal => Beginners => Topic started by: CapelliC on October 15, 2020, 12:13:20 pm

Title: [ok] is it possible to default value for a parameter of type 'array of const' ?
Post by: CapelliC on October 15, 2020, 12:13:20 pm
Since I use frequently the following pattern, I wonder if is there a way to declare a default value for a parameter of type 'array of const' as empty:

Code: Pascal  [Select][+][-]
  1. program x_array_of_const_default;
  2.  
  3. uses SysUtils;
  4.  
  5. {$if 0}
  6. procedure test(const msg: string);
  7. begin
  8.   writeln(msg)
  9. end;
  10. procedure test(const msg: string; const args: array of const);
  11. begin
  12.   test(format(msg, args))
  13. end;
  14. {$else}
  15. procedure test(const msg: string; const args: array of const = []);
  16. begin
  17.   writeln(format(msg, args))
  18. end;
  19. {$endif}
  20. begin
  21.   test('hello %s!', ['world']);
  22.   test('hello all!')
  23. end.
  24.  
  25.  

I realize could be impossible to avoid the error message (x_array_of_const_default.lpr(15,62) Fatal: Syntax error, ")" expected but "=" found), but I'd like to know from more knowledgeable people...

Thanks, Carlo
Title: Re: is it possible to default value for a parameter of type 'array of const' ?
Post by: egsuh on October 15, 2020, 12:46:50 pm

Well you have to use overload, I guess.
Title: Re: is it possible to default value for a parameter of type 'array of const' ?
Post by: Thaddy on October 15, 2020, 02:35:50 pm
varargs misuse will do that.
Title: Re: is it possible to default value for a parameter of type 'array of const' ?
Post by: jamie on October 19, 2020, 06:04:19 pm
That would be a nice feature..

but not sure exactly how that would look in a complex manner.


(a:Arary of const = ['hi',45,23,'done']);
(:a:array of byte = [1,2,3,4];);

Maybe we'll see this in the future
Title: Re: is it possible to default value for a parameter of type 'array of const' ?
Post by: PascalDragon on October 20, 2020, 10:02:43 am
Since I use frequently the following pattern, I wonder if is there a way to declare a default value for a parameter of type 'array of const' as empty:

No, there is not.
Title: Re: [ok] is it possible to default value for a parameter of type 'array of const' ?
Post by: CapelliC on October 27, 2020, 04:11:54 pm
Thanks you all for your answers. Appreciated.
TinyPortal © 2005-2018