[Grace-core] Class syntax and type parameters

James Noble kjx at ecs.vuw.ac.nz
Fri Nov 14 00:37:29 PST 2014


So, apropo of Michael's message about e.g. using "postcircumfix []";
(we'll have to put that perlism into the spec, if we go this way)
Andrew's email about classes, and Marco's question about generic _methods_
I wondered if the postcircumfix trick could work for classes - or at least class-like objects:

the code is below: basically one writes

box.new(i)                      to get a Box (aka Box[Unknown] initialised to i
box[Number].new(23)     to get a Box[Number}

so this syntax lines up quite well with the postcircumfix type syntax.

This does provide a reason for keeping dotted classes
(Michael's arguments this morning at least half reconvinced me to
head back to undotted classes-as-methods)
and the "class macro syntax" would get a bit odder...

class box[T].new(init} {
    var value : T := init 
    method get -> T {value}
    method set(newValue : T) -> Done {value:=newValue}
} 

could expand to something like:

def box = object {
    method new(init) typed(T) {
        object {
            var value : T := init 
            method get -> T {value}
            method set(newValue : T) -> Done {value:=newValue}
        }
    }
    
    method new(i) {new(i) typed(Unknown)} 
    method [](t) {
          object {
              method new(i) {outer.new(i) typed(t)}
          }
    }
}




def b1 = box.new("hello") //new Unknown box

def b2 = box[Number].new(23) //new box of Numbers




More information about the Grace-core mailing list