A Quick Tour of Boo

If you’re looking for a portable .NET language and don’t want to use a C-style syntax, you might want to take a look at Boo. Don’t let the name spook you; if you’re comfortable with Python, you’ll feel right at home with Boo.

Introduction

While the de facto .NET languages C# and Visual Basic .NET are certainly capable, only the former is especially portable (in practice, that is), and users coming from scripting backgrounds or users who don’t enjoy C-style syntax may not feel at home. Fortunately, however, the Common Language Infrastructure allows support for multiple languages, and these languages can be used together as the developer sees fit. Boo is one such language for the CLI. It offers the features of the .NET Framework and Mono, and it features a Python-like syntax. In this article, we’ll explore the Boo language and some of its basic features.

Boo can be obtained at:

http://boo.codehaus.org

The Boo Toolbox

Boo comes with three basic tools for working with Boo code. The first is the Boo compiler, which compiles Boo code into an assembly. It goes by the name of booc:

$ booc HelloWorld.boo

Boo Compiler version 0.7.8.2559 (CLR v2.0.50727.42)

The next tool is the Boo interpreter. Instead of compiling a Boo file, you can interpret it using booi:

$ booi HelloWorld.boo

Hello World!

It’s also possible to interpret code directly form standard input:

$ booi -

print "Hello World!"

print "Goodbye!"

Hello World!

Goodbye!

The final tool is the Boo interactive interpreter, booish, similar to Python’s interactive interpreter. As its name implies, it allows one to interact with it, immediately seeing the results of code, among other things:

$ booish

Welcome to booish, an interpreter for the boo programming language.

 

Running boo 0.7.8.2559 in CLR v2.0.50727.42.

 

The following built-in functions are available:

  dir(Type): lists the members of a type

  help(Type): prints detailed information about a type

  load(string): evals an external boo file

  globals(): returns the names of all variables known to the interpreter

  getRootNamespace(): namespace navigation

 

Enter boo code in the prompt below.

>>> print "Hello World!"

Hello World!

>>>

{mospagebreak title=Program Structure and Variables}

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.

{mospagebreak title=Arrays and Collections}

In Boo, arrays are defined a little differently from the way they are defined in other languages. Here, we create an array of integers and an array of strings:

ints = array(int,4)

ints[0] = 3

ints[1] = 6

ints[2] = 8

ints[3] = 90

strings = array(string, ["one", "two", "three"])

Note how the type of the array’s elements is passed, followed by either the size of the array or the array’s elements.

One very useful feature of arrays is slicing, which Python users will recognize. Slicing provides an easy way to extract a portion of an array:

theArray = [1, 2, 3, 4, 5]

print theArray[1:]

print theArray[1:3]

print theArray[2:]

[2, 3, 4, 5]

[2, 3]

[3, 4, 5]

Boo has support for two built-in collections, lists (for which slicing also works) and hashtables:

a = [4, 8, 15, 16, 23, 42]

a[0] = 8

a[1] = 15

a.Add(16)

b = {"jdoe": "John Doe", "jsmith": "John Smith"}

b["msmith"] = "Mary Smith"

However, the two collections are not type-safe:

c = [1,2]

c.Add("three")

d = {1: "one", "two": 2}

Fortunately, Boo has recently added support for generics, so one can create a type-safe collection using the collections in System.Collections.Generic:

import System.Collections.Generic

 

names = List of string()

names.Add("John")

names.Add("Sam")

names.Add("Mary")

 

rooms = Dictionary[of int,string]()

rooms.Add(100, names[0])

rooms.Add(200, names[1])

rooms.Add(300, names[2])

{mospagebreak title=Conditionals and Loops}

Boo’s if statement is similar to Python’s, accompanied by an elif statement and and else statement. No parentheses are needed around the condition being tested, and the body of each branch, which is intended, is introduced by a colon:

n = 3

if n % 2:

  print "Odd."

else:

  print "Even."

 

rating = 7

if rating >= 7:

  print "Good."

elif rating >= 4:

  print "Average."

else:

  print "Bad."

Odd.

Good.

Boo also features an unless statement, which executes the associated code unless the condition is met:

password = "ginger"

unless password == "licorice":

print "You can’t come in."

You can’t come in.

Boo provides a while loop with no surprises:

n = 5

nFactorial = 1

while n > 1:

  nFactorial = nFactorial * n

  n = n – 1

print nFactorial

120

However, Boo’s for loop is equivalent to a foreach loop in languages such as C#. It iterates through an enumerator:

for n in [4,8,15,16,23,42]:

  print n

4

8

15

16

23

42

In order to obtain the functionality of a traditional for loop, one has to iterate over the sequence of numbers that would be passed through the for loop. This sequence of numbers can be generated by range:

for n in range(5):

  print n

0

1

2

3

4

The range method can also take arguments specifying where the sequence should end and by what the starting point should be incremented to obtain the next number:

for n in range(1,3):

  print n

print

for n in range(1,3,2):

  print n

1

2

 

1

{mospagebreak title=Functions and Types}

Unlike C#, a method in Boo does not necessarily have to belong to a class. These functions provide an alternative to defining static methods that don’t fit in too well with any class. They are defined using the def keyword. For example, here is a function that squares an integer and then returns the result:

def square(x as int) as int:

  return x*x

Notice how we explicitly assign int as the return type of our function. While this is a good idea for complex functions, it’s possible to omit return type and make Boo determine it at compile-time. The following function works exactly the same as the one above:

def square(x as int):

  return x*x

The only difference is that it took a few less keystrokes to define the latter function.

A void method can be created the same way — with or without void specified as the return type. The following two functions are equivalent:

def a() as void:

  print "This is a void function."

def b():

  print "This is a void function."

To define a class in Boo, use the class keyword. Constructors go by the name of constructor. For example, here is a class that prints a string when initialized:

class TestClass:

  def constructor():

    print "TestClass initialized."

 

test = TestClass()

TestClass initialized.

Here is a class named Person whose constructor accepts a string, a name, and an int, age. Both values are then assigned to fields:

class Person:

  private _name as string

  private _age as int

 

  def constructor(name as string, age as int):

    _name = name

    _age = age

Now, of course, in order to access these fields, we need to create properties that correspond to them. Here, we create two properties, Name and Age, each providing a way to get and set the value:

  Name as string:

    get:

      return _name

    set:

      _name = value

 

  Age as int:

    get:

      return _age

    set:

      _age = value

Of course, this seems like overkill just to define two simple properties. Fortunately (and this is where it gets interesting), Boo provides a much neater way to do this. Instead of creating the property for a field explicitly, one can have Boo do it. Here, we recreate the Person class, compacting it into fewer lines:

class Person:

  [Property(Name)]

  private _name as string

 

  [Property(Age)]

  private _age as int

 

  def constructor(name as string, age as int):

    _name = name

    _age = age

As you can see, this makes things a lot easier on the developer. The class is considerably shorter but still quite readable. However, let’s say we want to make Name a read-only property. Boo provides a short way of doing this as well:

  [Getter(Name)]

  private _name as string

Interfaces may be created with the interface keyword. Method signatures that do not specify a return type are assumed to be void:

interface IBuyable:

  def GetPrice() as double

  def Buy()

To inherit from a class or to implement an interface, simply specify the name of the class or interface in parentheses:

class Pencil(IBuyable):

  def GetPrice():

    return 0.50

  def Buy():

    print "You bought a pencil!"

Conclusion

Though other .NET languages will get the job done, Boo may require less effort to work with, and it certainly will save the developer keystrokes. For example, rather than packing a program into a class, one can immediately start typing instructions, and rather than drawing out properties into multiple lines, one can neatly compact a simple property into two lines. Boo’s Python-style syntax and effort-saving features certainly merit consideration of the language.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan