Just a thought: read-only fields

Sergey Gromov snake.scaly at gmail.com
Tue Aug 4 04:46:30 PDT 2009


Mon, 3 Aug 2009 22:04:51 -0400, Nick Sabalausky wrote:

> It's been established in the recent epic-discussions on properties that one 
> of the biggest uses for properties is to implement publically read-only (but 
> privately-writable) fields. That got me thinking, why not actually have real 
> publically read-only fields instead of merely emulating them with 
> properties? They are, after all, a fairly common idiom.
> 
> // *Not* an actual syntax proposal, but just to get the idea across:
> 
> private @publicread int foo;
> 
> // The class sees it as "int", but everything else sees it as "const(int)"
> // ...or something like that...

Um... @publiconst? XD

or

private int foo;
public alias const foo foo;

or even

private public(const) int foo;

Weird...  OTOH this should be trivial from the implementation POV.  On
the even other hand this only worth considering if actual properties are
dropped because properties are more generic and you *can* implement a
read-only field with them.  Maybe some syntactic sugar is in order if it
really is such a common thing.



More information about the Digitalmars-d mailing list