Consider the following code:
Simba Code:
program new;
type TFoo = record
foo: integer;
end;
type TBar = record
bar: TFoo;
end;
procedure TFoo.doSomething();
begin
end;
function TBar.getFoo(): TFoo;
begin
result := self.bar;
end;
var
baz: TBar;
procedure test();
begin
baz.getFoo().doSomething();
end;
begin
test();
end.
This code in its current state will produce this:
Code:
Error: Variable expected at line 25
Compiling failed.
Change line 25 to this:
And and we are able to compile successfully.
Code:
Compiled successfully in 1550 ms.
Can anybody explain this to me?
Edit:
Changing line 11 to:
Simba Code:
procedure TFoo.doSomething(); constref;
will achieve successful compilation without editing line 25. However, in practice, appending constref; to many typed functions across a large code base seems to be problematic, as it produces other compilation errors in several places elsewhere in my include. This does not seem to be the cleanest solution, surely there must be another way?
Edit 2:
Here's an example of what I mean when I say adding constref; causes issues elsewhere. If we refine the example code at the top of this post to be the following:
Simba Code:
program new;
type TFoo = record
foo: integer;
end;
type TBar = record
bar: TFoo;
end;
procedure TFoo.doSomething(); constref;
begin
self.foo := 1234; // Added this line, changed nothing else
end;
function TBar.getFoo(): TFoo;
begin
result := self.bar;
end;
var
baz: TBar;
procedure test();
begin
baz.getFoo().doSomething();
end;
begin
test();
end.
I now recieve the error
Code:
Error: Target cannot be assigned to at line 13
Deleting the constref; puts me right back where I started, with a "Variable expected" error. Seems like it's either "Variable expected" when constref; is omitted, or "Target cannot be assigned to" when constref; is included.
What is going on?