[Grace-core] Semantics of def/variable access

Kim Bruce kim at cs.pomona.edu
Fri Jul 13 14:25:32 PDT 2012


Richard and I were having a discussion on the meaning of readable/writeable on variables (and defs).  I wanted to make sure we are all on the same page as to the semantics, given James example.

Rather than giving an abstract syntax, I'd like to give a possible implementation technique.  I hope you'll be able to tell me whether this gives the correct semantics.

Let's do the simple case first.
	var x:T is readable, writeable = obj{...}
This can be converted to essentially 
       var _x:T = obj{...} 
       method x -> T {_x}
       method x:=(x':T)->Done {_x := x'}
where _x is a generated name that cannot be written by a programmer and is guaranteed to be unique.  The default access to the methods is confidential (i.e., only accessible via self.x, super.x in the class and subclasses/objects}.  If the variable is annotated as public then both methods are declared public.

If a variable is readable, but not writeable then things are a bit more complex.  If we have private methods, then the x:=() method could be marked private.  If not, all updates of x would have to be translated to updates of _x.

I had originally thought that with a def that we wouldn't need to generate a new identifier, but writing
      method x-> T {obj{...}}
wouldn't necessarily give the right semantics as the body of the method would not be evaluated until it was invoked, rather than when it was defined.

Thus def's could be handled as above, where writeable is no longer a legal annotation.

The advantage of thinking of these translations is that we now have unambiguous semantics (I believe).

If we write the following code:

class C.new{
    var x:Number is readable := 17
    method m -> Number {x}
}

Class D.new{
   method x -> Number is overriding {super.x+1}
   method n -> Done {print(m)}
}

Then evaluating
   D.new.n 
clearly should print out 18 (as does the current minigrace implementation)

Have I misunderstood anything here?

Kim





More information about the Grace-core mailing list