[fpc-devel] FPC_HAS_CONSTREF

Martin fpc at mfriebe.de
Fri Oct 22 17:04:34 CEST 2010


On 22/10/2010 15:56, Martin wrote:
> On 22/10/2010 15:24, Jonas Maebe wrote:
>> On 22 Oct 2010, at 15:09, Martin wrote:
>>
>>> On 22/10/2010 12:28, Jonas Maebe wrote:
>>>>> AFAIK "constref" was invented for passing values to external 
>>>>> subroutines, that expect a reference. In how far does "constref" 
>>>>> affect the *caller*, in contrast to e.g. "var"? Can constref pass 
>>>>> properties, which are not allowed as var parameters?
>>>>
>>>> Right now it's allowed (if the property uses a getter, the getter 
>>>> is called and its result is put into a temp location whose address 
>>>> is passed), but I think that's a bug in the implementation.
>>> Why?
>> Because it's not possible to pass a reference to a property.
> True, but that only matters if you intend to change the value of the 
> property, by changing the value that the reference points to.
> const and constref do not make the promise that the refereed value can 
> be used to change the original value

In other words, as I understand (correct me if needed):

you are not passing the property ever:
   property FooP: TSomeType;
   var FooV: TSomeType;

procedure Bar(SomeFoo TSomeType);
Bar(FooP);
- you are passing the value of the property
Bar(FooV);
- you are passing the value of the variable, not the variable itself

procedure Bar(const SomeFoo TSomeType);
procedure Bar(constref SomeFoo TSomeType);
Bar(FooP);
- you are passing a reference to the value of the property (not a 
reference to the property)
Bar(FooV);
- you are passing a reference to the value of the variable
   it just happens to be a reference to the same memory that is used by 
the variable itself. This is because it's an optimization, the compiler 
made due to the hint it was given

procedure Bar(var SomeFoo TSomeType);
- In this case a reference to the variable is needed, so it can be modified.





More information about the fpc-devel mailing list