[fpc-devel] Parameters must match exactly?

Michael Van Canneyt michael at freepascal.org
Mon May 17 10:43:51 CEST 2010



On Mon, 17 May 2010, Graeme Geldenhuys wrote:

> Michael Van Canneyt het geskryf:
>>
>> You only need var/out when you want to change the instance pointer, i.e.
>> switch to another instance. And I count FreeAndNil() among this. (.Free
>> on the other hand is still possible, but not recommended).
>>
>> If  you just want to set properties or call methods on an existing instance,
>> there is no need to use out or var.
>
>
> Thank you Michael, I understand it much better now. Could you please add
> this information into the FPC Language Reference document? I re-read
> Chapter 11 in ref.pdf and there is no mention of what you said, and the
> documentation doesn't cover what happens if a Class is passed as a
> parameter. It only mentions simple types and arrays as parameters. Maybe
> adding a extra section in that chapter explaining when to use what?

I will do that.

>
> This brings me to another question. What is supposed to happen if you pass
> a Class instance as a const parameter? Does that even make sense? If not,
> why does the compiler allow that? Again, the documentation doesn't cover
> this scenario. Same question applies to passing a record structure.
>
> eg:
> ----------------------------
> procedure ProcessPerson(const APerson: TPerson);
> begin
>  ...  // Am I allowed to change APerson.Name property?
>  ...  // Am I allowed to assign a new instance to APerson?
> end;
>
> ...
>  lPerson := TPerson.Create;
>  lPerson.Name := 'Graeme Geldenhuys';
>  ProcessPerson(lPerson);
>
> ----------------------------
>
>
> * I see I can't assign a new instance to APerson - this makes sense.
> * I can assign new values to APerson.Name - am I supposed to be
>  allowed to do that?

Yes. In the same way as when you have a const pointer to a array of char,
you can change the array's contents, but not the pointer.

const means: you cannot change the value of the passed parameter.

Const doesn't make a lot of sense for pointers, since, with some
imagination, passing a pointer is equivalent to a var parameter.
(see C and other such languages)

Michael.



More information about the fpc-devel mailing list