Structural types
Use structural typing to write code that works
with objects with similar features but no common inheritance and interface
declarations. Define structural types similar to defining interfaces,
by specifying the common properties and method signatures. However, use
the structure keyword,
not the interface keyword.
Structural types are weaker than interfaces with respect to the amount of enforced type information they have in them. However, their flexibility supports situations where interfaces are ineffective or impossible. Structural types extend static typing to include a broader set of real-world situations but still support concise code that catches common coding problems at compile time.
Gosu is a statically typed language employing a nominal type system. A nominal type
system is one in which type assignability is based on declared type names, type
ancestry, and declared interfaces. For instance, in Gosu a type called Test1
is assignable to interface Interface1 only if Test1 declares
Interface1 in its hierarchy explicitly.
Another form of static typing called structural typing determines assignability
based on declared type features, such as methods and properties. The type called
B is structurally assignable to a structural type StructA
if B has compatible versions of all the methods and properties of StructB.
The type B does not have to formally declare that it implements
StructA. Structural typing is more flexible than nominal typing because it
measures a type based on its capability, not its name or inheritance declarations.
