A Quick Tour of Boo - Program Structure and Variables (Page 2 of 5 )
Unlike C# and Visual Basic.NET, Boo does not require a class to be declared with a static method as the program's entry point. A basic "Hello World" program can be written in a single line:
print "Hello World!"
Here, print is actually a macro. Boo users are free to develop their own macros as substitutes for repetitive code. Using the print macro is the same as calling the built-in print method:
print("Hello World!")
To access a namespace's contents without having to specify the namespace itself, import is used:
import System
Console.WriteLine("Hello World!")
Python programmers should take note: although import looks exactly the same as its Python equivalent, it isn't. Python's import allows the developer to find and access Python modules, while Boo's import merely provides a shortcut to a namespace's types, which could otherwise be accessed by specifying the namespace in front of the target type. Boo may look similar to Python, but their similarities don't extend too deep - watch out, Python users!
Now let's look at defining variables in Boo. Boo is a statically typed language, so the types of variables are determined at compile-time. Let's define some variables of various types:
name as string = "John Doe"
gender as char = char('m')
married as bool = false
age as int = 35
savings as double = 12345.67
Note how we explicitly assign each variable a type. In some instances, such as when we define but don't immediately initialize a variable, this is required. However, Boo can usually determine the type of a variable at compile-time just by looking at what's assigned to it:
name = "John Doe"
gender = char('m')
married = false
age = 35
savings = 12345.67
Although Boo is a statically typed language, there exists a special type called duck that provides support for duck typing. That is, you're free to use a variable of type duck as you see fit, without the compiler watching your back at compile-time:
x as duck
x = 4
x = "four"
x = false
The above code assigns an integer value to x, followed by a string and then false. Normally, this wouldn't compile, but with Boo's duck typing, it does. The above example is poor, however, since the same effect could be achieved by declaring x as type object. Consider this next example, then, where we use methods associated with specific types:
x as duck
x = 4
x = x.MinValue
x = "string"
x = x.Replace("s", "q")
Note, though, that Boo is still a statically typed language and that duck typing should not generally be used.
Next: Arrays and Collections >>
More BrainDump Articles
More By Peyton McCullough