[Grace-core] Class syntax and type parameters

Andrew P. Black black at cs.pdx.edu
Tue Nov 11 13:04:36 PST 2014


More on classes with type parameters.

Here is the use case that prompted the question that I raised in the teleconference yesterday.

factory method list<T> {
    inherits collectionFactory.trait<T>

    method withAll(a:Collection<T>) -> List<T> {
        object {
            inherits indexable.trait<T>
            var inner := _prelude.PrimitiveArray.new(a.size * 2 + 1)
            var size is public := 0
            for (a) do {x->
                inner.at(size)put(x)
                size := size + 1
            }
		…
         
Notice that it’s necessary for the type parameter to go on the constructor object `list` rather than on the method `withAll()`: if the parameter is on `withAll()`, then it’s nor accessible on line 2 where it is needed to parameterize the inherited trait.

So we have inconsistent naming:

	list<T>.withAll 

but

	collectionFactory.trait<T>

because the latter is declared with the class syntax, and therefor can only — by virtue of Michael Homers pronouncement — be parameterized as above.

This motivated by original question: does
	
	class a.b { object constructor body }

mean (1)

	def a is public = object {
		method b { 
			object { object constructor body }
		}
	}

or (2)

	method a  { 
		object {
			method b { 
				object { object constructor body }
			}
		}
	}

I had always assumed (1), but now (2) looks more reasonable.

	Andrew




More information about the Grace-core mailing list