Type Variables

The typecheck package provides an implementation of type variables, an idea borrowed from languages like Haskell.

Type variables are best explained by examples; here, we reimplement an earlier Self example using type variables:

class Foo:
	@accepts('a', Number, Number, 'a')
	@returns( ['a'] )
	def foo(self, a, b, c):
		...
		return our_list

Type variables are specified by strings (though we use single characters here, it is equally permissible to use longer, more descriptive strings) appearing in the typechecking decorators. In the following examples, we'll provide illustrations of several functions that satisfy the given signature, as well as several that do not.

@accepts('first_class', 'second_class')
@returns( ['first_class'], ['second_class'] )

# Passes
def foo(a, b):
	return [a], [b]

# Fails
def foo(a, b):
	return [b], [a]
	
# Passes, but only if a is a Number
def foo(a, b):
	return [5, 6, 7], [b]

Valid XHTML 1.0 Transitional