[Next] [Previous] [Up] [Top] [Contents] [Index]

4.1 Parameterized Declarations

4.1.2 Explicit and Implicit Parameterization

A type parameter is explicit if the corresponding instantiating type is to be explicitly provided by clients of the declaration, or implicit if it is to be inferred automatically by the typechecker. A polymorphic object or method declaration specifies which parameters are explicit by listing the corresponding type variables in brackets following the name of the declared entity. The explicitly instantiating types should be similarly given in brackets whenever the object, type, or message is referenced:

named_object	::=	name [params]
named_type	::=	name [params]
message	::=	msg_name [params] "(" [exprs] ")"
dot_msg	::=	dot_expr "." msg_name [params] ["(" [exprs] ")"]
params	::=	"[" types "]"

In the previous example, the i_vector object is explicitly parameterized and requires all clients to provide instantiating types, as in i_vector[num]. Method fetch, in contrast, is parameterized implicitly; in the expression fetch(my_vec, 5) the instantiating type for T is inferred to be num. Inference allows the programmer to avoid writing the often obvious instantiating types; it is a key feature of Cecil. It is described in more details in Section 4.3.

The number of explicit type parameters is considered part of the "name" of the declared entity. For example, multiple objects can be declared with the same name, as long as they are declared with different numbers of explicit type parameters.[14] Also, method lookup is extended to include the number of explicit parameters of candidate methods (which must match that at the callsite) as part of the method selection process. Method lookup does not depend on the instantiating types, however. For example, message sends foo[int]() and foo[string]() will always be dispatched to the same method implementation, but foo[int,string]() will be dispatched to a different implementation.


[14] This feature does not interact well with mixed dynamic and static typing, since the number of parameters affects the execution behavior of the program, violating the principle that static types do not affect the execution semantics. In the future, the number of parameters may be removed from the "name" of an object or method, so that parameters are confined to the (optional) static type system.