[Grace-core] Permitting multiple constructors and inheritance for classes

Andrew P Black andrew.p.black at gmail.com
Wed Feb 19 14:19:25 PST 2014


Trying again, without tabs

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