[Grace-core] Transparent Colours - Family Polymorphism in Grace?

James Noble kjx at ecs.vuw.ac.nz
Wed Jan 25 19:34:27 PST 2012


Hi all

this code (should be in sample code/colours2.grace) is my second attempt
at Andrew's example of transparent colours.  (second, after talking to Michael)

This does work in minigrace, but it cheats - mingrace's inherits is actually delegation 
(if you only inherit from classes, call new each time, you can never tell) 
& name resolution does't work properly across inheritance...



def Colour = object {

 method auxNew(r' : Number, g' : Number, b' : Number) {
   return object {
      def r = r'
      def g = g'
      def b = b'
      method asString {"Colour r:{r} g:{g} b:{b}"}
 } }

method red    { auxNew(1,0,0) }
method green  { auxNew(0,1,0) }
method blue   { auxNew(0,0,1) }
method yellow { auxNew(0,1,1) }
method white  { auxNew(1,1,1) }

}


def TransparentColour = object  {
  inherits Colour

 method auxNew(r : Number, g : Number, b : Number) {
   return object {
      inherits super.auxNew(r, g, b)
      def alpha = 1
      method asString {"Transparent {super.asString} alpha:{alpha}"}
 } }

}


print (Colour.yellow)
print (TransparentColour.yellow)



More information about the Grace-core mailing list