[Grace-core] Edited Variable & Constant page

James Noble kjx at ecs.vuw.ac.nz
Mon Nov 15 23:21:36 PST 2010


> Ah, I see the problem.  While reading the whole of that post, I  
> assumed that we were talking about temporary variables and  
> invariables.
> Now I have figured out that you are talking about instance variables  
> and invariables.

yes. or both!

> Maybe we should distinguish them syntactically?  This distinction is  
> a really important one to teach, and using the same syntax for two  
> very different concepts is arguably a bad idea.
>
> 	private field x
>
> or
> 	private field x: Number
>
> declares an instance variable that can be bound, and re-bound,  
> inside the object.

I quite like "field"! I was hoping there was something obvious,  
analogous to method.
Perhaps this is it (but see below...)

> private is optional, but it is the default if omitted.
>
> 	readonly field x
>
> is the same, except that it also builds a method called getX (oh, I  
> hate that 'get' stuff; it's the price of being able to omit self)  
> that can be sent to the object to access x's value.

no it doesn't need to be called getX!
we *do* need to sort this out: but both the "field x " and "readonly  
field x" declarations
make a method called "x" - calling that method reads the variable.

> 	readwrite field x: Number
>
> is the same, except that it also builds a method called setX() that  
> assigns it's argument to x.

I don't think there should be a difference between "field" and  
"readonly field".
here's how I think this single namespace thing should work:
either a field or a constant slot named "x" is read by a "method"  
called x
readwrite fields are also written by a method called   **something we  
haven't decided"
perhaps the easiest option is that method is called "set_x"  (ugly, I  
know)
or even "setX" - have to do the biCapitalisation automatically

then, all code of the form e1.id := e2    is rewritten to be  
e1.setX(e2).

the point is that, if you declare a pair of methods in an object:
  method x : Number
  method setX(Number n) : void

clients (and subclasses) are unable to distinguish this from a field  
declaration



> And then we can have private const x, readonly const x, etc. for the  
> invariables.

so the keywords would be "field" and "const" - yep that could work.

but I don't understand what a "readonly const" is?

the other difficultly with "field" is that I was hoping to use the  
*same* syntax
for declaring fields in objects  and locals in blocks.  Here, this  
follows
Self and Beta (and arguably Simula)

the difficulty with "const" is, well
  readonly const x = HighlyVolatileMutableClass.new;   // urgh. How is  
this const?



another option, that may be unpopular, is that these things are all  
just "var" iables

var x = e   declares a fixed binding
var x := e  declares a variable binding.

or even just x = e    vs x :=  e   (which is pretty close to self)


J


More information about the Grace-core mailing list