Compile-time error prevention
Static typing allows you to detect most type-related errors at compile time. This increases reliability of your code at run time. This is critical for real-world production systems. When the Gosu editor detects compilation errors and warnings, it displays them in the user interface as you edit Gosu source code.
For example, functions (including object methods) take parameters and return a value. The information about the type of each parameter and the return type is known at compile time. During compilation, Gosu enforces the following constraints:
- Calls to this function must take as parameters the correct number of parameters having the appropriate types.
- Within the code for the function, code must always treat an object as the appropriate type. For example, you can call methods or get properties from the object, but only methods or properties declared for that compile-time type. It is possible to cast the value to a different type, however. If the run-time type is not a subtype of the compile-time type, run-time errors can occur.
- If code that calls this function assigns a variable to the result of this function, the variable type must match the return type of this function.
For example, consider the following function definition.
public function getLabel(person: Person) : String {
return person.LastName + ", " + person.FirstName
}
If you write code that calls this method and passes
an integer instead of a Person,
the code fails with a type mismatch compiler error. The compiler recognizes
that the parameter value is not a Person,
which is the contract between the function definition and the code that
calls the function.
Similarly, Gosu ensures that all property
access on the Person object
(LastName and FirstName properties) are valid
properties on the class definition of Person.
If the code inside the function calls any methods on the object, Gosu
also ensures that the method name that you are calling actually exists
on the type.
Within the Gosu editor, violations of these rules become compilation errors. By finding this large class of problems at compile time, you can avoid unexpected run-time failures.
