[Grace-core] Permitting multiple constructors and inheritance for classes
Andrew P Black
andrew.p.black at gmail.com
Wed Feb 19 14:12:03 PST 2014
Reading these proposals, one might infer that we don't like nesting!
An alternative, which james and I discussed in Wellington, is to use a keyword, or keyword-sequence to
declare a method that returns a fresh object. (We could use class method for this, but here I'm going to use
factory method).
So
def myClass = object {
method with(*a) { self.withAll(a) }
factory method withAll(a) {
var inner := PrimitiveArray.new(a.size * 2 + 1)
var size is readable := 0
for (a) do {x->
inner.at(size)put(x)
size := size + 1
}
method boundsCheck(n) is confidential {
if ((n < 1) || (n > size)) then {
boundsError.raise "index {n} out of bounds 1..{size}"
}
}
method at(n) {
boundsCheck(n)
inner.at(n-1)
}
method at(n)put(x) {
boundsCheck(n)
inner.at(n-1)put(x)
self
}
....
}
}
translates into
def myClass = object {
method with(*a) { self.withAll(a) }
method withAll(a) {
return object {
var inner := PrimitiveArray.new(a.size * 2 + 1)
var size is readable := 0
for (a) do {x->
inner.at(size)put(x)
size := size + 1
}
method boundsCheck(n) is confidential {
if ((n < 1) || (n > size)) then {
boundsError.raise "index {n} out of bounds 1..{size}"
}
}
method at(n) {
boundsCheck(n)
inner.at(n-1)
}
method at(n)put(x) {
boundsCheck(n)
inner.at(n-1)put(x)
self
}
....
}
}
}
More information about the Grace-core
mailing list