Recent

Author Topic: [solved] how to do a default string array parameter  (Read 1626 times)

Joanna

  • Hero Member
  • *****
  • Posts: 764
[solved] how to do a default string array parameter
« on: December 19, 2023, 11:44:14 am »
Hi everyone
I am trying to create a procedure that will use a default string array when no array is provided;

i want the default array to be fairly empty like this:
Code: Pascal  [Select][+][-]
  1. const empty_array:array[0..0] of string = ('');  

when i try to declare the procedure however it doesn't compile..

Code: Pascal  [Select][+][-]
  1. PROCEDURE INIT (CONST TBL_name:ShortString;CONST DATA_TYPE:TDATATYPE_CATEGORIES;const caps:TStringArray = empty_array );

I'm not sure of the syntax. I've only used default parameter with very simple values before. is it possible to do this or do i need to use overloaded init procedures, one with an array and one without ?
« Last Edit: December 19, 2023, 12:07:10 pm by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

korba812

  • Sr. Member
  • ****
  • Posts: 394
Re: how to do a default string array parameter
« Reply #1 on: December 19, 2023, 11:52:55 am »
If the type TStringArray is a dynamic array then you can use the value "nil" as the default value.

Code: Pascal  [Select][+][-]
  1.     PROCEDURE INIT (CONST TBL_name:ShortString;CONST DATA_TYPE:TDATATYPE_CATEGORIES;const caps:TStringArray = nil );

Joanna

  • Hero Member
  • *****
  • Posts: 764
Re: how to do a default string array parameter
« Reply #2 on: December 19, 2023, 12:06:29 pm »
works perfectly, thanks so much i can't believe i didn't think of using nil  %)
Code: Pascal  [Select][+][-]
  1. PROCEDURE INIT (CONST TBL_name:ShortString;CONST DATA_TYPE:TDATATYPE_CATEGORIES;const caps:TStringArray = nil );
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [solved] how to do a default string array parameter
« Reply #3 on: December 19, 2023, 01:12:09 pm »
Apart from SHOUTING formatting, I would never  call a procedure INIT, because it is also tied in the language as a semi-reserved word for a constructor in TP mode. Which can clash if you need that mode.
« Last Edit: December 19, 2023, 08:16:46 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: [solved] how to do a default string array parameter
« Reply #4 on: December 19, 2023, 02:19:42 pm »
(iirc    = [] works in trunk too)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [solved] how to do a default string array parameter
« Reply #5 on: December 19, 2023, 08:18:13 pm »
(iirc    = [] works in trunk too)
() objfpc. You refer to mode delphi.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: [solved] how to do a default string array parameter
« Reply #6 on: December 19, 2023, 09:38:54 pm »
I am trying to create a procedure that will use a default string array when no array is provided;

An important rule when dealing with default parameters: the default value can only be an untyped constant. So anything like your empty_array - which is a typed constant - is simply not valid. The only valid constant value for dynamic arrays is Nil as you learned by now.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: [solved] how to do a default string array parameter
« Reply #7 on: December 19, 2023, 09:56:15 pm »
(iirc    = [] works in trunk too)
() objfpc. You refer to mode delphi.

I always refer to mode Delphi. :D

Joanna

  • Hero Member
  • *****
  • Posts: 764
Re: [solved] how to do a default string array parameter
« Reply #8 on: December 20, 2023, 05:09:57 am »
Sorry Thaddy  :-* .. there are quite a few things in this project that would not work well with turbo pascal... especially the gui  :D

Although in my use case, the nil array works best it still is an interesting idea to have more complex things as default parameters. I tried using the address of a const array and that didn’t work either..
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [solved] how to do a default string array parameter
« Reply #9 on: December 20, 2023, 02:17:22 pm »
Joanna, mode objfpc is not mode TP.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018