[fpc-pascal] Variable arguments, different types?

Michael Van Canneyt michael at freepascal.org
Thu Mar 9 17:46:46 CET 2006



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.



More information about the fpc-pascal mailing list