Boo is a statically typed language.
Static typing is about the ability to type check a program for type correctness.
Static typing is about being able to deliver better runtime performance.
Static typing is not about putting the burden of declaring types on the programmer as most mainstream languages do.
The mechanism that frees the programmer from having to babysit the compiler is called type inference.
Type inference means you don't have to worry about declaring types everywhere just to make the compiler happy. Type inference means you can be productive without giving up the safety net of the type system nor sacrificing performance.
Boo's type inference kicks in for newly declared variables and fields, properties, arrays, for statement variables, overriden methods, method return types and generators.
Assignments can be used to introduce new variables in the current scope. The type for the new variable will be inferred from the expression on the right.
s1 = "foo" # declare new variable s1
s2 = s1.Replace("f", "b") # s1 is a string so Replace is cool
|
Only the first assignment to a variable is taken into account by the type inference mechanism.
The following program is illegal:
s = "I'm a string" # s is bound with type string s = 42 # and although 42 is a really cool number s can only hold strings |
class Customer:
_name = ""
|
Declare the new field _name and initialize it with an empty string. The type of the field will be string.
When a property does not declare a type it is inferred from its getter.
class BigBrain: Answer: get: return 42 |
In this case the type of the Answer property will be inferred as int.
The type of an array is inferred as the least generic type that could safely hold all of its enclosing elements.
a = (1, 2) # a is of type (int)
b = (1L, 2) # b is of type (long)
c = ("foo", 2) # c is of type (object)
|
names = (" John ",
" Eric",
" Graham",
"TerryG ",
" TerryJ",
" Michael")
for name in names:
# name is typed string since we are iterating a string array
print name.Trim() # Trim is cool, name is a string
|
This works even when with unpacking:
a = ( (1, 2), (3, 4) )
for i, j in a:
print i+j # + is cool since i and j are typed int
|
When overriding a method, it is not necessary to declare its return type since it can be safely inferred from its super method.
class Customer:
override def ToString():
pass
|
The return type of a method will the most generic type among the types of the expressions used in return statements.
def spam():
return "spam!"
print spam()*3
# multiply operator is cool since spam() is inferred to return a string
# and strings can be multiplied by integer values
|
def ltuae(returnString as bool):
return "42" if returnString
return 42
# ltuae is inferred to return object
print ltuae(false)*3 # ERROR! don't know the meaning of the * operator
|
When a method does not declare a return type and includes no return statements it will be typed System.Void.
g = i*2 for i in range(3)
# g is inferred to generate ints
for i in g:
print i*2 # * operator is cool since i is inferred to be int
# works with arrays too
a = array(g) # a is inferred to be (int) since g delivers ints
print a[0] + a[-1] # int sum
|
When implementing interfaces it's important to explicitliy declare the signature of a method, property or event. The compiler will look only for exact matches.
In the example below the class will be considered abstract since it does not provide an implementation with the correct signature:
namespace AllThroughTheDay interface IMeMineIMeMineIMeMine: def AllThroughTheNight(iMeMine, iMeMine2, iMeMine3 as int) class EvenThoseTears(IMeMineIMeMineIMeMine): def AllThroughTheNight(iMeMine, iMeMine2, iMeMine3): pass e = EvenThoseTears() |
Let's say when.
abstract def Method(param /* as object */, i as int) as string:
pass
def fat([required(value >= 0)] value as int) as int:
return 1 if value < 2
return value*fat(value-1)
for i as int in [1, 2, 3]: # list is not typed
print i*2
|
def foo() as object: # I want the return type to be object not string
# a common scenario is interface implementation
return "a string"
if bar:
a = 3 # a will be typed int
else:
a = "42" # uh, oh
|
f as System.IDisposable = foo() f.Dispose() |
def CreateInstance(progid): type = System.Type.GetTypeFromProgID(progid) return type() |
ie as duck = CreateInstance("InternetExplorer.Application")
ie.Visible = true
ie.Navigate("http://boo.codehaus.org/Type+Inference")
|