Friday, July 2, 2010

OO Hello World - Cobra



Hello world in Cobra is here! Another Python – Like language which has lots of built in cool features such as Unit Tests and Contracts.


NOTE: The indentation appears to be 1 space when it should be 4. Cobra won't accept anything other than 4 (or one tab per indent). Take it in consideration if you want to test the code :)


By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.

class Greet
 var name
 
 cue init(name as String)
   base.init
   #.name = name[0].toString.toUpper + name[1:]
   .name = name.capitalized
  
  def salute
   print "Hello [.name]!"

# Greet the world!
class GreetProgram
  def main
   g = Greet("world")
   g.salute

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.

use System
namespace _GreetProgram

 class Greet
  var name as String
  
  cue init(name as String)
   base.init
   #.name = name[0].toString.toUpper + name[1:]
   .name = name.capitalized
  
  def salute
   print "Hello [.name]!"


use _GreetProgram
 
# Greet the world!
 class GreetProgram
  def main is shared
   g = Greet("world")
   g.salute

The Program Output:









Cobra Info:
“Cobra is a general purpose programming language with: a clean, high-level syntax, static and dynamic binding, first class support for unit tests and contracts, compiled performance with scripting conveniences, lambdas and closures, extensions and mixins... and more” Taken from: (http://cobra-language.com/)

Appeared:
2006
Current Version:
Developed by:
Chuck Esterbrook
Creator:
Chuck Esterbrook
Influenced by:
Python (Guido van Rossum), C# (Anders Hejlsberg), Eiffel (Bertrand Meyer), Objective-C (Tom Love, Brad Cox)
Predecessor Language
Predecessor Appeared
Predecessor Creator
Runtime Target:
CLR
Latest Framework Target:
2.0
Mono Target:
Yes
Allows Unmanaged Code:
No
Source Code Extension:
“.cobra”
Keywords:
139 (including types)
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:
No
Latest IDE Support:
SharpDevelop
MonoDevelop
VisualStudio
Language Reference:
Extra Info:


1 comment:

  1. Wonderful information your website gives the best and the most interesting information. Great job you people are doing posting nice content........................Please contact us for Oracle Fusion Financials training details in our Erptree Training Institute

    ReplyDelete