BrainDump
  Home arrow BrainDump arrow Page 5 - A Quick Tour of Boo
CIO Insight
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
eWeek
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

A Quick Tour of Boo
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-08-06

    Table of Contents:
  • A Quick Tour of Boo
  • Program Structure and Variables
  • Arrays and Collections
  • Conditionals and Loops
  • Functions and Types

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    The Best Selling PC Migration Utility.
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    A Quick Tour of Boo - Functions and Types
    (Page 5 of 5 )

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello, all,I've always found Python's syntax particularly pleasing, but, at the...
     

       

    BRAINDUMP ARTICLES

    - Outsourcing: the Hoopla, the Reality
    - MySQL Plays in the Sun
    - All About SQL Functions
    - SQL: Functioning in the Real World
    - More Advanced SQL Statements
    - Beginning SQL the SEQUEL: Working with Advan...
    - Beginning SQL
    - A Look at the VI Editor
    - A Quick Tour of Boo
    - Book Review: Open Source Licensing
    - PGP and GPG: Email for the Practical Parano...
    - Microsoft Continues War on Open Source
    - Secure Remote Desktop Sharing with VNC on Li...
    - A Look at Google Project Hosting
    - What we can Learn from Two Linux vs. Microso...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway