[Grace-core] Inheritance and Template Objects

James Noble kjx at ecs.vuw.ac.nz
Fri Apr 5 04:46:54 PDT 2013


So here are "Classiifed" versions of andrew's examples. 

		class aMatch.successful(b, m) {
				inherits true.template

				def bindings is public = b
				def matched is public = m
		}

this assumes true has a "template" method which is basically clone or something very close.
Hmm,  think I'd prefer the name "trait" for that method to "template"

		class emptyness.template {
				method size is abstract {}
				method isEmpty { self.size == 0 }
				method notEmpty { self.isEmpty.not }
				method ifEmpty(block) { self.isEmpty.ifTrue(block) } 
				method ifNotEmpty(block) { self.isEmpty.ifFalse{block.apply(self)}} 
				method ifEmpty(block)ifNotEmpty(block2){ {self.isEmpty.ifTrue(block)ifFalse{block2.apply(self)}} 
		}

turned it into a class, constructor method called "template" :-)
Again: I quite like the cognate "class emptyness.trait { " 

		class aSet.with(args*) {
				inherits emptiness.template + enumerable.template
				def rep = aList.withCollection(args*)   // don't have infix splat expansion
				method size { rep.size }
		}

all straightforward

		def aCartesianPoint = object {
			method x(a)y(b) { 
				object {
					def x is public = a
					def y is public = b 
					print "nothing will be executed until an object is made from this template"
				}
			}  


inner "template" becomes an "object" - the same as Andrews other example below.
Either way the message is printed - but no object is "thrown on the floor" - when
this method is called from the "inherits" clause of the colorPoint object the message
is printed (and everything else happens) in the context of the new object.


		class colorPoint.x(x)y(y)color(c) {
				inherits aCartesianPoint.x(x)y(y)
				def color = c
			}


similarly, the interval example: 

> 	def upInterval = object anInterval.from 0 to 10 by 2
> 	def downInterval = object anInterval.from 10 to 0 by (-2)
> 
	class anInterval.from(f)to(l)by(s) {
                                        inherits enumerable.template
                                        def first = f
					def last = l	
					def step = s
					method do (block) { if (s > 0) then ...  }
				}
			}
	}

	def anInterval = object {
			method from(f)to(l)by(s) {
				if s > 0 then {
					object {
						inherits enumerable.template
						def first = f
						def last = l	
						def step = s
						method do (block) { ... // code for upward interval }
					} 
				} else {
					object {
						inherits enumerable
						def first = f
						def last = l	
						def step = s
						method do (block) { ... // code for upward interval }
					}
			}
						

> Now the template that I get back from from()to()by(s) depends on the value of s, but since they both have the same template type, everyone should be happy.

actually I think you said templates were nominal, so they wouldn't have the same "template type".  They would, however, have the same _type_ - which is all that matters.

Note that in our system one couldn't inherit from anInterval.from()to()by, because it isn't static.
But that's probably the same for you too?

James


More information about the Grace-core mailing list