[Grace-core] Class syntax

Kim Bruce kim at cs.pomona.edu
Thu Jul 18 17:41:45 PDT 2013


> 
> 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.

This the preferred way of doing it, except I would instead write:

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

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

This convention (and it is only a convention) makes it clearer that you have a single class Cat, but with two constructors.

The way I think about it, we have reduced the functionality of classes so that they are just extensible constructors.  This is illustrated well in the above example.  On the other hand, both generate objects with the same type, and these objects can be used interchangeably.  That is harder to use in Java unless you also use interfaces.

Presumably, even without the names, a good optimizer would recognize that the method suites are the same and use the same dispatch table.
> 
> Thoughts?
> 
> Daniel
> 
> 
> _______________________________________________
> Grace-core mailing list
> Grace-core at cecs.pdx.edu
> https://mailhost.cecs.pdx.edu/mailman/listinfo/grace-core



More information about the Grace-core mailing list