[Grace-core] Inheritance and object initialisation

Kim Bruce kim at cs.pomona.edu
Tue Jul 24 18:36:53 PDT 2012


> 
> As to use cases, there is a very nice example of inheriting form objects in Michael's matching code, where he inherits from true and false.
> The other example that we are all familiar with is when one class inherits from another.  Classes are singleton objects.  So, for example, a class 
> 
> 	def theColor = object {
> 		method black { theColor.r(0)g(0)b(0) }
> 		method blue { theColor.r(0)g(0)b(1) }
> 		method magenta { theColor.r(1)g(0)b(1) }
> 		...
> 		method r(red)g(green)b(blue) {  object { def r = red; def g = green; def b = blue
> 										... 
> 		}
> 	}
> 
> that provided named colors might be extended to crate one that also provides named grey shades
> 
> 	def colorsAndGreys = object {
> 		inherits theColor
> 		method darkGrey { greyScale(0.2) }
> 		method midGrey { greyScale(0.5) }
> 		method lightGrey { greyScale(0.8) }
> 		method greyScale(fraction) {  r(fraction)g(fraction)b(fraction) }
> 		...
> 	}
> 
> 	Andrew
> 

Of course the case we're having problems with involve mutable objects.  Inheriting from immutable objects can be easily done by treating them as classes with parameterless constructors.

Any good examples of inheritance using mutable objects (ideally where the state has changed since it was created)?

Kim


More information about the Grace-core mailing list