ProtocolsStatic Duck Typing

Protocols provide a way to achieve duck typing in Gosu without sacrificing static verification. This functionality is achieved by introducing Protocol Types into Gosu via The Open Type System.


Protocols are defined in files ending in the .proto suffix. An example protocol definition is:

package example protocol ExampleProtocol { function aFunction() : String }

This protocol can now be used like so:

var proto : example.ExampleProtocol proto = new SomeClassThatHasAFunctionOnIt() proto.aFunction() proto = new SomeOtherUnreleatedClassThatHasAFunctionOnIt() proto.aFunction()

The two classes above, which both have an aFunction():String method defined, both satisfy the protocol definition and, thus, can be assigned to the protocol variable. This is despite the fact that the two types are totally unrelated. If either class didn't have aFunction():String defined then the code above would have a compilation error: the Protocol type verifies that the other type conforms to it at the point of assignment.