Martin Atkins (mart) wrote in fotobilder,
Martin Atkins

S2 Language Proposal: Read-only Data Members (and also, hidden stuff naming convention)

Sorry. I'm madly switching back and forth between compiler and core as I go over my Sent Items mail folder looking at all of the old stuff I sent. I don't think I actually sent this as a proposal directly, but I was reminded of it when reading over an old proposal for changing the Color class on LJ, which turned out to be not such a good idea.

The Color class has some ‘internal’ members which store the current red, green and blue values for the color. They're named with an underscore prefix right now, which I think is bad because (as I've proposed before) I feel that names starting with underscores should be reserved for the use of the backend. ($_ctx is used to represent the S2 context for the perl backend, for example. That variable name in particular is currently reserved, but it'd be better to generalize it to allow both for future expansion and also for classes handled in the builtin layer (like Color) to store ‘hidden’ data members.) One current application of hidden data members is that the ItemRange class could store a simple URL template in a hidden member for use with its url_of() method.

However, we can't really hide the red, green and blue values because they're needed for generating color codes in formats other than HTML's (since S2 should theoretically be able to generate anything), so let's make them regular variables but not allow S2 code to assign to them, only the backend. This means that their underscore prefix can be removed, and ensures that they'll never end up containing an invalid color value (anything outside the range 0‒255). It also ensures that the string member containing the HTML color (also readonly) will always be in sync. A final nicity is that the docstring viewer thingy can automatically put “Read-only” in front of the docstrings for read-only members, so that people know not to try to set them.

The syntax can be simple enough…

class Color {
  var readonly int r;
  var readonly int g;
  var readonly int b;
  var readonly string as_string;
  # and the methods, afterwards

The S2 compiler can then simply throw an exception whenever it encounters an assignment operator where the left operand is a read-only data member. The read-only keyword should obviously only be valid for class member variables. It just means that the variables have to carry an extra boolean value in their type representation in the compiler. I could probably implement the assignment-trapping part myself, but I'm not too sure on dealing with the readonly keyword because I've never fooled with anything in the compiler that isn't an operator before.


  • 302: lj_dev

    In the interests of consolidating all FotoBilder development-related discussion, we're going to be closing down this community. The same…

  • Development stalled?

    Is the development of Fotobilder held? Stalled? I am asking because there is no activity on the community and there is no link to the Fotobilder…

  • (no subject)

    Does FotoBilder works with Apache2? I installed all the required modules on my debian sarge, and when I restart my apache server, it dies horribly…

  • Post a new comment


    Comments allowed for members only

    Anonymous comments are disabled in this journal

    default userpic