I get a strange compiler error whenever I use an inline directive on a method.
The following code demonstrates this:
type
Program = class
private
FValue: Integer;
public
method GetValue: Integer; inline;
end;
implementation
method Program.GetValue: Integer;
begin
result := FValue; // Visibility error here
end;
When compiling this code with Oxygene 9.0.97.2071 I get the following error:
Member “Program.FValue” is less visible than inlined method “method Program.GetValue(): Integer” it is used from
It goes away if I remove the inline directive. But I think even with the inline it is completely legitimate code and should not cause any compile errors.
Unfortunately, inline is a compiler feature, it’s inlined at the caller and the net and Java runtimes won’t allow foreign code access to private fields
PMFJI: The behaviour of the underlying compilers can be argued as reasonable (by inlining the method, the visibility of the referenced members must be consistent with the inlined context). You can of course argue it the other way as well. But I guess it is what it is.
That being the case unless this is an excessively simplified example it looks to me as if this is really just a publicly readable privately writable member, so why not simply:
type
Program = class
public
property Value: Integer public read private write;
end;
If the real-world case is more complex than this, then you could presumably still achieve the desired result by provisioning a publicread-only property to read fValue and use that property in your public inlined method:
type
Program = class
private
fValue: Integer;
public
property Value: Integer read fValue;
method SomethingThatUsesValue; inline;
begin
var twiceTheValue := Value * 2;
end;
end;
After having run into one more issue related to an inlined method, we have decided to add {$IFNDEF oxygene} around all inline statements in our source code. More or less every time Oxygene has been updated with a new major release we have had problems with inlined methods. Stuff that works in Delphi and also in the previous version of oxygene.
Maxim and I are both working on the same code. I do the Delphi part, then he adapts it to Oxygene where needed.