Home arrow BrainDump arrow Page 5 - A Quick Tour of Boo

Functions and Types - BrainDump

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.

TABLE OF CONTENTS:
  1. A Quick Tour of Boo
  2. Program Structure and Variables
  3. Arrays and Collections
  4. Conditionals and Loops
  5. Functions and Types
By: Peyton McCullough
Rating: starstarstarstarstar / 4
August 06, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More BrainDump Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

BRAINDUMP ARTICLES

- Apple Founder Steve Jobs Dies
- Steve Jobs` Era at Apple Ends
- Google's Chrome Developer Tool Updated
- Google's Chrome 6 Browser Brings Speed to th...
- New Open Source Update Fedora 13 is Released...
- Install Linux with Knoppix
- iPad Developers Flock To SDK 3.2
- Managing a Linux Wireless Access Point
- Maintaining a Linux Wireless Access Point
- Securing a Linux Wireless Access Point
- Configuring a Linux Wireless Access Point
- Building a Linux Wireless Access Point
- Migrating Oracle to PostgreSQL with Enterpri...
- Demystifying SELinux on Kernel 2.6
- Yahoo and Microsoft Create Ad Partnership

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: