Hello,
I’m trying to initialize read only props with .ctr as follows:
namespace SimpleClassTest;
type
SimpleClass = public class
public
property MyReadOnlyProp1: Integer; readonly; required;
property MyReadOnlyProp2: Integer read private write; required;
end;
Program = class
public
class method Main(args: array of String): Int32;
begin
var lSimpleClass := new SimpleClass(
MyReadOnlyProp1 := 10,
MyReadOnlyProp2 := 20
);
end;
end;
end.
But compiler raises an error
E: Property "MyReadOnlyProp1" on type "SimpleClass" is read-only [W:\_TMP_\RemObjects Samples\Examples\SimpleClassTest\Program.pas (19)]
E: member "MyReadOnlyProp2" on type "SimpleClass" cannot be called because it is private [W:\_TMP_\RemObjects Samples\Examples\SimpleClassTest\Program.pas (20)]
The problem is, (at least on .NET), a lot of this is enforced at runtime too (at least the private-write; not sure about readonly). So the class would actually have to implement a .ctor that takes all properties as potential parameters. And .NET has pretty strict rules for how .ctors ar inherited, so “just always” introducing a hidden .ctor that can be used for this would introduce all kinds of issues (not to mention, maybe would allow setting properties you dont want a caller to set – which really is what readonly is for ;).
I have opened a ticket to investigate the issue; we just need to find a good syntax to represent the equivalent of { get; init; } in Oxygene. Ideally w/o introducing a new keyword.
I think it’s not mandatory to define new keyword, why it’s not enough the “readonly” one changing the behavior of init using the already defined syntax?
What do you think is the case of inconsistent meaning using that?
P.S. sorry for my poor ability to explain what I’m trying to mean
Because right now readonly implies that only the constructor itself (i.e. code tha is under control of the class author) can set these properties. If we made allreadonly properties behave like this, that will allow outside-of-the-class code to set values that are expected to not settable like that.