Allow writing to readonly fields from same class/struct

Moderator: GZDoom Developers

Post Reply
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Allow writing to readonly fields from same class/struct

Post by argv »

It would improve ZScript's safety if struct/class fields could be made “readonly”, but still be written to from code that has private-level access to the field (i.e. is part of the same struct/class). That way, code elsewhere can't mistakenly write to those fields, but they can still be given a value. As it is, non-native readonly fields appear to be completely useless, since they can never be assigned any value.

Alternatively, perhaps they should be given behavior along the lines of Java “final” fields: the Init method can write to them, but they can never be written from anywhere else.
User avatar
Chris
Posts: 2940
Joined: Thu Jul 17, 2003 12:07 am
Graphics Processor: ATI/AMD with Vulkan/Metal Support

Re: Allow writing to readonly fields from same class/struct

Post by Chris »

argv wrote:Alternatively, perhaps they should be given behavior along the lines of Java “final” fields: the Init method can write to them, but they can never be written from anywhere else.
That's basically how const class members work in C++. They can be initialized/written to in the constructor's initializer list, but anywhere else is an error.
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: Allow writing to readonly fields from same class/struct

Post by argv »

On that note, proper initializers/constructors would be nice. I'm guessing they were left out for a reason, though.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Allow writing to readonly fields from same class/struct

Post by Graf Zahl »

'readonly' means 'readonly', not 'private'! If you want somerthing different, name it differently!
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”