On Thu, Oct 27, 2005 at 09:54:10PM +0200, Adriaan van Os wrote:
Consider the following test program.
program test;
type Str255 = record sLength: Byte; sChars: packed array[1..255] of Char end;
operator + ( protected var s1, s2: Str255 ) = theResult : Str255; var len1, len2: Integer; begin len1 := s1.sLength; len2 := Min( 255 - len1, s2.sLength); theResult.sLength := len1 + len2; if len1 > 0 then theResult.sChars[1..len1] := s1.sChars[1..len1]; if len2 > 0 then theResult.sChars[len1+1..len1+len2] := s2.sChars[1..len2]; end;
var s1: Str255 value ( sLength: 1, sChars: 'A'); s2: Str255 value ( sLength: 1, sChars: 'B'); s3: Str255 value ( sLength: 1, sChars: 'C'); s4: Str255;
begin s4:=s1+s2+s3 { Error: reference expected, value given }; writeln( s4.sChars[ 1..s4.sLength]) end.
The program triggers an error for s1+s2+s3. The error doesn't occur when the expression has only one + operator (e.g. s1+s2). The problem disappears when using const parameters. It looks like the internal representation of an intermediate result is triggering the compiler error !
Regards,
Adriaan van Os
This has nothing to do with internal representation, it's just the usual Pascal syntax rules. If a function (or operator) has a var parameter, you should feed it a reference. Variables s1 and s2 are references, thus s1+s2 is OK. The expression s1+s2 itself is not a reference, thus (s1+s2)+s3 is invalid. Const parameters are syntactically value parameters (though they are implemented as call by reference), thus these restrictions do not apply to them.
Emil Jerabek