Type parameters

Graydon Hoare graydon at mozilla.com
Tue Sep 5 16:53:02 PDT 2006


Peter Hall wrote:

> I don't think this is correct. It is true that, inside methods of Api,
> you are only going to use methods of Required. But without type
> restrictions Api couldn't call *any* methods without a lot of
> unpleasant runtime tests and coercions. 

Perhaps I am mis-parsing this sentence. It sounds like you're saying:

   'Api' is only going to want to use methods of 'Required', and
    this is no good because 'Api' can only use methods of 'Required'.

If that's what you're saying, I don't understand the problem. Api can 
only call the methods it's interested in. Seems fine to me. I'm probably 
misunderstanding.

> usefully parametrize it with several subtypes of Required, and gain
> the benefit of type-safety when using the subtype functionality -
> which is the whole point of parametrized types after all. 

I hate to sound like the broken record here, but can you give a concrete 
example? I'm looking for a case that satisfies all these properties:

   - A real idiom you see a lot
   - That needs a parametric type
   - That needs the parametric type to carry a type bound
   - That can't get by -- just as safely -- with helper functions

Perhaps this is just my own language bias. I've done more ML and C++ 
than Java 5, but my hunch is that Java only included it because they 
lack function types: they habitually shoe-horn every form of abstraction 
into a form of inheritance.

> feeling is that lack of type restriction consigns parametrized types
> to be used only for dumb collections (or objects that could be
> constructed equivalently by aggregating such a collection).

"Dumb collections" are one of the most universal programming idioms. You 
would find it hard to write an ecmascript program that didn't at least 
use the array type. We want to support typed arrays. Further, most 
useful programs contain hashtables or sorted maps. We can type those 
precisely by adding opaque type parameters. The universality of need and 
limited complexity of the feature makes it seem like a worthwhile purchase.

I don't get the same feeling from type-parameter bounds. Maybe I just 
haven't seen the canonical example yet. I'm happy to reconsider my 
position if there's some Very Important Idiom that's inexpressible 
without them. I'm just not interested in supporting the feature for the 
sake of completeness. The language design is already "too complete".

-graydon



More information about the Es4-discuss mailing list