Sign in Join

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

Mailing lists / fpc-pascal / 2006-03

Variable arguments, different types?

13 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

L505

Hi, This below seems to work.. I got the impression you couldn't do this.

program HelloWorld;
var
  I : Integer;
procedure test (args : array of const);
begin
 writeln('test');
  For i:=0 to High(Args) do
  begin
    case Args[i].vtype of
      vtstring:
        Writeln ('String value: ',args[i].vSTRING^);
      vtinteger:
        Writeln ('Integer value: ',args[i].vINTEGER);
    end;
  end;
end;
begin
  test([1, 'test']); // variable types work
  readln;
end.

In other words you can reinvent your own writeln style procedures and pass parameters of *different types* simutaneously. I was under the impression you could only pass parameters of the same type through this [] square bracket trickery.

Reply by email

Michalis Kamburelis
to L505: Hi, This below seems to work.. I got the impression you couldn't do this. program…

(Second send, it seems that mails from my old email address do not reach fpc lists)

L505 wrote:
[...]

Show quoted text 6 lines
In other words you can reinvent your own writeln style procedures and pass parameters of *different types* simutaneously. I was under the impression you could only pass parameters of the same type through this [] square bracket trickery.

See docs: [http://www.freepascal.org/docs-html/ref/refsu47.html]

Michalis

Reply by email

L505
to Michalis Kamburelis
> > In other words you can reinvent your own writeln style procedures and pass > parameters of *different types* simutaneously. I was under the impression

you

Show quoted text 6 lines
> could only pass parameters of the same type through this [] square bracket > trickery. > See docs: [http://www.freepascal.org/docs-html/ref/refsu47.html]

I already RTFM actually :-)

My point was that the docs do not state that you can send multiple types within one shot.

  test([1, 1.456, 'test', -64, 'some string]);

I have also read people stating things like this before:

"you can use array of const but you can't make functions like writeln because writeln accepts multiple types".

But in fact, with array of const, you can use multiple types.

Note the difference between:

  test(['string1', 'string2', 'string3]);  // all the same types

And:

  test([1, 1.456, 'test', -64, 'some string]); // different types

Reply by email

Vinzent Hoefler
to L505
L505 wrote
I have also read people stating things like this before: "you can use array of const but you can't make functions like writeln because writeln accepts multiple types".

Usually the statement is about different _numbers_ of arguments, not different types. Writing subroutine which accept different types for their arguments has never been a problem in Pascal.

But in fact, with array of const, you can use multiple types.

You can, just as you can use different types in record types. Still, the actual argument type would be the type of the record or, as in your case "array of const" then. Don't confuse that.

And you still can't just pass two arguments when the subroutine expects only one (yes, other trickery can be done with overloading and default arguments).

Vinzent.

Reply by email

L505
to Vinzent Hoefler
Show quoted text 9 lines
> I have also read people stating things like this before: > > "you can use array of const but you can't make functions like writeln > because writeln accepts multiple types". Usually the statement is about different _numbers_ of arguments, not different types. Writing subroutine which accept different types for their arguments has never been a problem in Pascal.

I was talking specifically about array of const, not Pascal in general. The docs do not make it clear that array of const can accept different types in "one call". We are talking purely about "array of const" here, not the Pascal language in general. Of course I know the Pascal language can accept multiple types in one call.

Show quoted text 6 lines
> But in fact, with array of const, you can use multiple types. You can, just as you can use different types in record types. Still, the actual argument type would be the type of the record or, as in your case "array of const" then. Don't confuse that.

Well you are nitpicking my email :-). I'm not confused at all - the docs are. All I was asking was why specifically in array of const's case, the docs did not make it clear I could pass multiple types in the same call. If you look in the examples in the docs page, all the examples show the programmer passing arguments of the same type in one call.

They only make it clear that you can pass multiple types in separate calls, not in the same call.

// passing multiple types, but in separate calls
test([TRUE, FALSE, TRUE]);
test(['string1', 'string2', 'string3']);
// passing MULTIPLE TYPES IN ONE CALL, not stated anywhere in docs
test([TRUE, 'string1', 342, -456, 1.653]);
And you still can't just pass two arguments when the subroutine expects only one

Isn't this exactly what array of const is for? It allows you to use anywhere from 1 to unlimited parameters.

Reply by email

Michael Van Canneyt
to L505
L505 wrote 26 lines
> >> I have also read people stating things like this before: >> >> "you can use array of const but you can't make functions like writeln >> because writeln accepts multiple types". > > Usually the statement is about different _numbers_ of arguments, not > different types. Writing subroutine which accept different types for > their arguments has never been a problem in Pascal. I was talking specifically about array of const, not Pascal in general. The docs do not make it clear that array of const can accept different types in "one call". We are talking purely about "array of const" here, not the Pascal language in general. Of course I know the Pascal language can accept multiple types in one call. > >> But in fact, with array of const, you can use multiple types. > > You can, just as you can use different types in record types. > Still, the actual argument type would be the type of the record or, as in > your case "array of const" then. Don't confuse that. Well you are nitpicking my email :-). I'm not confused at all - the docs are.

As the author of the docs, I feel compelled to protest:

"This is a special case of the Open array construction, where it is allowed to pass any expression in an array to a function or procedure."

The "any expression" is the keyword here...

But, to make it more explicit, I have added
"Each element of the array can have a different type."

And have added some examples where the elements have different types.

Michael.

Reply by email

Michael Van Canneyt
to L505: ----- Original Message ----- From: "Michael Van Canneyt" To: "FPC-Pascal users…
L505 wrote 41 lines
>>>> I have also read people stating things like this before: >>>> >>>> "you can use array of const but you can't make functions like writeln >>>> because writeln accepts multiple types". >>> >>> Usually the statement is about different _numbers_ of arguments, not >>> different types. Writing subroutine which accept different types for >>> their arguments has never been a problem in Pascal. >> >> I was talking specifically about array of const, not Pascal in general. The docs >> do not make it clear that array of const can accept different types in "one >> call". We are talking purely about "array of const" here, not the Pascal >> language in general. Of course I know the Pascal language can accept multiple >> types in one call. >> >>> >>>> But in fact, with array of const, you can use multiple types. >>> >>> You can, just as you can use different types in record types. >>> Still, the actual argument type would be the type of the record or, as in >>> your case "array of const" then. Don't confuse that. >> >> Well you are nitpicking my email :-). I'm not confused at all - the docs are. > > As the author of the docs, I feel compelled to protest: > > "This is a special case of the Open array construction, where it is > allowed to pass any expression in an array to a function or procedure." > > The "any expression" is the keyword here... > > But, to make it more explicit, I have added > "Each element of the array can have a different type." > > And have added some examples where the elements have different types. Sorry Michael, my emails sound a bit harsh some times.

? No offense was taken :-)

I was just wondering if this was actually supposed to be possible?

Yes, that is the purpose. It makes the 'Format' function possible, and keeps typesafety.

I don't think I've ever seen anyone use this trick before. It's kind of like Perl. Dangerous, weak typing. I wonder how slow it is since it is a run time check :-)

There is no weak typing and no run time check: the types of the arguments are known at compile time. Just read the docs: The procedure that receives the argument gets a record with variant part.

The compiler ensures that this record is correct at compile time.

Michael.

Reply by email

Vinzent Hoefler
to L505: I was talking specifically about array of const, not Pascal in general. The docs do not…
L505 wrote
Well you are nitpicking my email :-).

Maybe. I'm known for doing that sometimes. I also even tend to write "SizeOf (byte)" instead of "1".

I'm not confused at all - the docs are.

Are they?

"My" Free Pascal: Reference Guide states:

|10.3.6 Array of const

[...]

|Inside the procedure body, the array of const is equivalent to an open |array of TVarRec:
|
|Procedure Testit (Args: Array of const);
|
|Var I : longint;
|
|begin
[...]
| For i:=0 to High(Args) do
| begin
| write ('Argument ',i,' has type ');
| case Args[i].vtype of
| vtinteger :
| Writeln ('Integer, Value :',args[i].vinteger); | vtboolean :
| Writeln ('Boolean, Value :',args[i].vboolean); | vtchar :
| Writeln ('Char, value : ',args[i].vchar); | vtextended :
| Writeln ('Extended, value : ',args[i].VExtended^); | vtString :
| Writeln ('ShortString, value :',args[i].VString^); [...]

Show quoted text 5 lines
All I was asking was why specifically in array of const's case, the docs did not make it clear I could pass multiple types in the same call. If you look in the examples in the docs page, all the examples show the programmer passing arguments of the same type in one call.

That's true. But looking at - for instance - SysUtils.Format should make it clear, that an "array of const" is not limited to that.

They only make it clear that you can pass multiple types in separate calls, not in the same call.

So you propose, the examples and maybe the text

|This is a special case of the Open array construction, where it is |allowed to pass any expression in an array to a function or procedure. ^^^^^^^^^^^^^^

should be changed then? :-)

Show quoted text 5 lines
> And you still can't just pass two arguments when the subroutine > expects only one Isn't this exactly what array of const is for? It allows you to use anywhere from 1 to unlimited parameters.

Yes. Still, it's only one argument. You can't just suddenly pass two "array of const", can you?

Vin"Nitpicker"zent.

Reply by email

L505
to Michael Van Canneyt: As the author of the docs, I feel compelled to protest: "This is a special case of the…

----- Original Message -----

Earlier message 57 lines
From: "Michael Van Canneyt" To: "FPC-Pascal users discussions" Sent: Thursday, March 09, 2006 9:36 AM Subject: Re: [fpc-pascal] Variable arguments, different types? > > > On Thu, 9 Mar 2006, L505 wrote: > > > > > > >> > >>> I have also read people stating things like this before: > >>> > >>> "you can use array of const but you can't make functions like writeln > >>> because writeln accepts multiple types". > >> > >> Usually the statement is about different _numbers_ of arguments, not > >> different types. Writing subroutine which accept different types for > >> their arguments has never been a problem in Pascal. > > > > I was talking specifically about array of const, not Pascal in general. The docs > > do not make it clear that array of const can accept different types in "one > > call". We are talking purely about "array of const" here, not the Pascal > > language in general. Of course I know the Pascal language can accept multiple > > types in one call. > > > >> > >>> But in fact, with array of const, you can use multiple types. > >> > >> You can, just as you can use different types in record types. > >> Still, the actual argument type would be the type of the record or, as in > >> your case "array of const" then. Don't confuse that. > > > > Well you are nitpicking my email :-). I'm not confused at all - the docs are. > > As the author of the docs, I feel compelled to protest: > > "This is a special case of the Open array construction, where it is > allowed to pass any expression in an array to a function or procedure." > > The "any expression" is the keyword here... > > But, to make it more explicit, I have added > "Each element of the array can have a different type." > > And have added some examples where the elements have different types. Sorry Michael, my emails sound a bit harsh some times. I was just wondering if this was actually supposed to be possible? I don't think I've ever seen anyone use this trick before. It's kind of like Perl. Dangerous, weak typing. I wonder how slow it is since it is a run time check :-)

Reply by email

Vinzent Hoefler
to L505: Okay but why would you need that (humor me). LOL
L505 wrote 7 lines
> > Isn't this exactly what array of const is for? It allows you to > > use anywhere from 1 to unlimited parameters. > > Yes. Still, it's only one argument. You can't just suddenly pass > two "array of const", can you? Okay but why would you need that (humor me).

Hmm. You're right, it wouldn't be needed. You could pass the second array of const as element of the first one and implement the called routine as a recursive one. :-)

Vinzent.

Reply by email