Saturday, October 23, 2010

Cobra - Basics by Example



Continue with the Basics by Example; today's version of the post written in Cobra Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 



Greetings Program - Verbose
# Cobra Basics
use System
namespace CobraGreetProgram

    class Greet is public
        # Fields or Attributes
        var __message as String
        var __name as String
        var __loopMessage as int
        #Properties
        pro message as String is public
            get
                return __message
            set
                __message = .capitalize(value)
        pro name as String is public
            get
                return __name
            set
                __name = .capitalize(value)
        pro loopMessage as int is public
            get
                return __loopMessage
            set
                __loopMessage = value
        # Constructor
        cue init is public
            base.init
            __message = ''
            __name = ''
            __loopMessage = 0
        # Overloaded Constructor
        cue init(message as String, name as String, loopMessage as int) is public
            base.init        
            __message = message
            __name = name
            __loopMessage = loopMessage
        # Method 1
        def capitalize(val as String) as String is private
            # 'if-then-else' statement
            if val.length >= 1
                return val.capitalized
            else
                return ''
        # Method 2
        def salute is public
            # 'for' statement 
            for i in __loopMessage
                print '[__message] [__name]!'
        # Overloaded Method 2.1
        def salute(message as String, name as String, loopMessage as int) is public
            # 'while' statement 
            i as int = 0
            while i < loopMessage
                print '[.capitalize(message)] [.capitalize(name)]!'
                i+=1
        # Overloaded Method 2.2
        def salute(name as String) is public
            # 'switch/case' statement is not supported  
            # using branch statement instead  
            dtNow as DateTime = DateTime.now
            branch dtNow.hour
                on 6 or 7 or 8 or 9 or 10 or 11, __message = 'good morning,'
                on 12 or 13 or 14 or 15 or 16 or 17, __message = 'good afternoon,'
                on 18 or 19 or 20 or 21 or 22, __message = 'good evening,'
                on 23 or 0 or 1 or 2 or 3 or 4 or 5, __message = 'good night,'
                else, __message = 'huh?'
            print '[.capitalize(__message)] [.capitalize(name)]!'

    # Console Program
    class Program is public
        def main is shared
            # Define object of type Greet and Instantiate. Call Constructor            
            g as Greet = Greet()
            # Call Set Properties
            g.message = 'hello'
            g.name = 'world'  
            g.loopMessage = 5
            # Call Method 2  
            g.salute
            # Call Overloaded Method 2.1 and Get Properties  
            g.salute(g.message, 'cobra', g.loopMessage)
            # Call Overloaded Method 2.2  
            g.salute('carlos')
              
            # Stop and Exit  
            print 'Press any key to exit...'
            Console.read
Greetings Program - Minimal
# Cobra Basics
class Greet
    # Fields or Attributes
    var __message
    var __name
    var __loopMessage
    #Properties
    pro message as String
        get
            return __message
        set
            __message = .capitalize(value)
    pro name as String
        get
            return __name
        set
            __name = .capitalize(value)
    pro loopMessage as int
        get
            return __loopMessage
        set
            __loopMessage = value
    # Constructor
    cue init
        base.init
        __message = ''
        __name = ''
        __loopMessage = 0
    # Overloaded Constructor
    cue init(message as String, name as String, loopMessage as int)
        base.init        
        __message = message
        __name = name
        __loopMessage = loopMessage
    # Method 1
    def capitalize(val as String) as String is private
        # 'if-then-else' statement
        if val.length >= 1
            return val.capitalized
        else
            return ''
    # Method 2
    def salute
        # 'for' statement 
        for i in __loopMessage
            print '[__message] [__name]!'
    # Overloaded Method 2.1
    def salute(message as String, name as String, loopMessage as int)
        # 'while' statement 
        i = 0
        while i < loopMessage
            print '[.capitalize(message)] [.capitalize(name)]!'
            i+=1
    # Overloaded Method 2.2
    def salute(name as String)
        # 'switch/case' statement is not supported  
        # using branch statement instead  
        dtNow = DateTime.now
        branch dtNow.hour
            on 6 or 7 or 8 or 9 or 10 or 11, __message = 'good morning,'
            on 12 or 13 or 14 or 15 or 16 or 17, __message = 'good afternoon,'
            on 18 or 19 or 20 or 21 or 22, __message = 'good evening,'
            on 23 or 0 or 1 or 2 or 3 or 4 or 5, __message = 'good night,'
            else, __message = 'huh?'
        print '[.capitalize(__message)] [.capitalize(name)]!'

# Console Program
class Program
    def main
        # Define object of type Greet and Instantiate. Call Constructor            
        g = Greet()
        # Call Set Properties
        g.message = 'hello'
        g.name = 'world'  
        g.loopMessage = 5
        # Call Method 2  
        g.salute
        # Call Overloaded Method 2.1 and Get Properties  
        g.salute(g.message, 'cobra', g.loopMessage)
        # Call Overloaded Method 2.2  
        g.salute('carlos')
          
        # Stop and Exit  
        print 'Press any key to exit...'
        Console.read

And the Output is:



Auto-Implemented Properties in Cobra
Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. The following code shows how to use them just like with VB.NET, C#, C++/CLI and so on.

pro <propname> [ as <type>] [from {var, <backingvariablename>} [= <initvalue>] ]
[<docstring>]


class Greet
    var __message
    var __name
    var __loopMessage
    #Properties
    pro message from __message
    pro name from __name
    pro loopMessage from __loopMessage
    # or you can just say from var as here below. it will look for
    # fields called (_|__)message and (_|__)name and (_|__)loopMessage
    # pro message from var
    # pro name from var
    # pro loopMessage from var
    #Constructor
    cue init
        base.init
        __message = ''
        __name = ''
        __loopMessage = 0    
    def capitalize(val as String) as String is private
        return val.capitalized
    def salute       
        print '[__loopMessage] [__message] [__name]!'

class Program
    def main
        g = Greet()        
        g.message = 'hello'
        g.name = 'world'  
        g.loopMessage = 5        
        g.salute
        Console.read

More about Properties in Cobra's wiki site: http://cobra-language.com/trac/cobra/wiki/Classes

And the Output is:






Unit Test and Contracts in Cobra

As stated in my series post, I'm covering strictly basics OO and other basics constructs on my code examples, but in Cobra's case I would like to add that the way you should be doing Cobra (or... the Cobra-nic style (like in Pythonic je...)) should be using Unit Tests and Contracts right from your code using test and require instructions. However, I will just show an example and keep the explanation for future posts.
Let's see an example

extend String

    def capitalize as String
        test
            assert ''.capitalize == ''
            assert 'x'.capitalize == 'X'
            assert 'X'.capitalize == 'X'
            assert 'cobrA'.capitalize == 'CobrA'
        body
            branch .length
                on 0, return this
                on 1, return .toUpper
                else, return this[0].toString.toUpper + this[1:]

class Greet

    cue init(name as String)
        require name.length > 0
        base.init
        _name = name.capitalize

    get name from var as String
    
    def salute
        print 'Hello [.name]!'


class GreetProgram
    """ Greet the world! """

    def main
        g = Greet('world')
        g.salute

No comments:

Post a Comment