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

James Noble kjx at ecs.vuw.ac.nz
Fri Jan 27 01:04:48 PST 2012


> Here is what I was thinking of while we were on the conference call.  Not very different, I think.  Does this compile?

this version does, in svn as colours3. The only important 
difference is the inner object constructor "inherits Colour.r(red) g(green) b(blue)"
i.e. names the super-inner class explicitly, rather than saying e.g.
rather than "outer.super.r(red) g(green) b(blue)" or something...

James

(and yes, the extra parens in the "red" etc methods do look horrid)


def Colour = object {

 method r(red') g(green') b(blue') {
     return object {
       def r = red'
       def g = green'
       def b = blue'
       method asString {"Colour r:{r} g:{g} b:{b}"}
       method renderOn(c) {print "Rendering as a Colour: {asString}"}
     }
 }

 method red    { r(1) g(0) b(0) }
 method green  { r(0) g(1) b(0) }
 method blue   { r(0) g(0) b(1) }
 method yellow { r(0) g(1) b(1) }
 method white  { r(1) g(1) b(1) }
 method black  { r(0) g(0) b(0) }
 method midGrey { r(0.5) g(0.5) b(0.5) }
}


def TransparentColour = object  {
 inherits Colour

 method r (red) g (green) b (blue) {
   return r(red) g(green) b(blue) alpha(1)
   } 

 method r(red) g(green) b(blue) alpha(t) {
   return object {
      inherits Colour.r(red) g(green) b(blue)
      def alpha = t
      method asString {"Transparent {super.asString} alpha:{alpha}"}
        method renderOn(c) {
                if (alpha == 1) then 
                        {super.renderOn(c)} 
                else
                        {print "Rendering as a TransparentColour: {asString}"}
        }               
   } 
 }
}


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




More information about the Grace-core mailing list