SignatureTyping

Last edit February 27, 2010
SignatureTyping, which could possibly also be known as StaticDuckTyping or StaticPolymorphism, statically checks an object's signature, rather than its actual type in the typechecking phase of compilation. A really good SignatureTyping language would infer from how you use the object what signature it has, allowing you to write code that looks like python, but is actually checked at compile time. The advantage to SignatureTyping is that you can have abstract signatures, like iterable, callable, or can-be-added-to-an-integer, rather than actual types, like ExternalIterator, FunctorObject, or Integer. The advantages of this are:
  • you can easily write polymorphic code, without typecasts, such as
    def apply(f : Callable, lst : Iterable):
         y as Duck = None
         lst.ToInternalIterator.each => x:
              y = f(y, x) if y is not None else x
    return y
    class SomeClass():
         def call(x, y):
              return x + y
    apply(SomeClass(), [1, 2, 3]) # => 6
    apply(SomeClass(), [(1, 2), (3, 4), (5, 6)]) # => (1, 2, 3, 4, 5, 6)
    apply((lambda x, y: print x; print y; y), (1, 2, 3, 4)) 
    # 1
    # 2
    # 2
    # 3
    # 3
    # 4
    # => 4