Typing
2010-12-11
Introduction
The type system is a very important aspect of a programming language. It has a major impact on the usability and flexibility of a language, affects the structure of programs and the way programmers thing about their programs, and affects the ability of the compiler to catch programming errors and perform optimizations.
Types
Mana provides a number of built-in types, and also allows new types to be defined.
Overloading
Mana supports overloading, meaning that it is possible to define multiple functions, each with their own type signatures, all using the same name.
Interfaces
In addition to types, Mana allows the definition of interfaces, which specify a set of functions, including type signatures. A type is said to implement an interface when it supports all methods specified for that interface. Interfaces allow functions to be specified on any type implementing a specific interface, without depending on the actual type.
Examples
## Define the interface printable
interface printable {
# The function print takes an argument of some type
# and returns a value of that same type
print 'a 'a
}
## Define the print function for strings
function print (x string) {
print-string x
}
## Define the print function for numbers
function print (x number) {
print-string (to-string x)
x
}
## Define the type person as a structure with
## a name field holding a string
struct person () {
name string
}
## Define the print function for persons
function print (x person) {
print-string (name x)
x
}
## Now we can make a list of printable objects and print them
loop for x in (list "hello" 42 (make-person "John")) do {
print x
print-newline
}