[Grace-core] Class syntax

Daniel Gibbs gibbsdani at myvuw.ac.nz
Thu Jul 18 17:28:22 PDT 2013


Hi all,

Just looking at the class syntax in Grace and wondering how friendly it is to novice programmers, and what exactly it is trying to teach them. Currently the following:

class Cat.new(name') {
    def name = name'
}

is essentially syntactic sugar for a Cat factory:

def Cat = object {
    method new(name') {
        object {
            def name = name'
        }
    }
}

Which is fine, but as soon as you want to have multiple constructors you have to write the whole factory explicitly:

def Cat = object {
    method new(name') {
        new(name')withColor("blue")
    }
    method new(name')withColor(color') {
        object {
            def name = name'
            def color = color'
        }
    }
}

How useful is this for teaching novices programming? Having a special class syntax for the case where you have one constructor, and then having to jump to this rather verbose factory structure doesn't seem particularly novice-friendly.

You could argue that novices will only have to use classes with single constructors until they've had more experience but some of the Java classes used in our introductory programming course have classes with multiple constructors, so this is not always true.

One way to achieve a similar effect to multiple constructors is to use inheritance (this currently works, I'm not sure if it matches the current definition of inheritance):

class ColoredCat.new(name')withColor(color') {
    def name = name'
    def color = color'
}

class Cat.new(name') {
    inherits ColoredCat.new(name')withColor("blue")
}

Although this approach seems to suggest that a new class needs to be created every time a new constructor is needed which probably isn't an ideal thing to teach novices.

Thoughts?

Daniel




More information about the Grace-core mailing list