Variable arguments, different types?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Variable arguments, different types?

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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Michalis Kamburelis
L505 wrote:
[...]
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Michalis Kamburelis-2
In reply to this post by L505
(Second send, it seems that mails from my old email address do not reach
fpc lists)

L505 wrote:
[...]
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

L505

> >
> > 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]
>


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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Vinzent Höfler
On Thursday 09 March 2006 04:31, 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

L505


>
> > 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.
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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Michael Van Canneyt


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.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

L505

----- Original Message -----
From: "Michael Van Canneyt" <[hidden email]>
To: "FPC-Pascal users discussions" <[hidden email]>
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 :-)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Michael Van Canneyt


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.

? 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Vinzent Höfler
In reply to this post by L505
On Thursday 09 March 2006 16:23, 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^);  
[...]

> 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? :-)

> > 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

L505

> > 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).

>
>
> Vin"Nitpicker"zent.
>

LOL
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

L505
In reply to this post by Michael Van Canneyt
I don't see what all the arguments are about the C language  being able to
rewrite writeln (printf, etc.) but in Pascal not. I don't see any limits with
what Pascal can do - you could rewrite writeln procedure and other low level
calls with several options - textrec tricks, or this array of const trick.

Let's put those Pascal myths to shame.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Variable arguments, different types?

Vinzent Höfler
In reply to this post by L505
On Thursday 09 March 2006 17:02, L505 wrote:
> > > 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal