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.
blog comments powered by Disqus |
|
|
|
|
|
|
|