Saturday, February 25, 2012

OO Hello World - Ceylon



Update 1: Porting code examples to Ceylon M5 - syntax changes on assignent operator, module definition and support for String Interpolation.

The OO Hello World in Ceylon, the "the new language from Red Hat for the JVM", is here!

Ceylon is a general-purpose, imperative, statically-typed, block-structured, object-oriented, higher-order language featuring a syntax similar to Java and C#, and a type system based on the notion of principal types.

You can see the OO Hello World series 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(String name) {   
    String _name = name[0..0].uppercased + name[1...];  
    shared void salute() {  
        print("Hello, ``_name``!");  
    }
} 
  
// Greet the world!  
void run() {  
    value g = Greet("carlos");  
    g.salute();  
}  

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
shared class Greet(String name) {   
    variable String _name = name[0..0].uppercased + name[1...];  
    shared void salute() {  
        print("Hello, ``this._name``!");  
    }  
}  
  
// Greet the world!  
shared void run() {  
    value g = Greet("carlos");  
    g.salute();  
} 

The Program Output:







Ceylon Info:

“Ceylon is a programming language for writing large programs in a team environment. The language is elegant, highly readable, extremely typesafe, and makes it easy to get things done. And it's easy to learn for programmers who are familiar with mainstream languages used in business computing. Ceylon has a full-featured Eclipse-based development environment, allowing developers to take best advantage of the powerful static type system. Programs written in Ceylon execute on any JVM.” Taken from: (http://www.ceylon-lang.org/ )

Appeared:
2011
Current Version:
Developed by:
Red Hat
Creator:
Gavin King
Influenced by:
Java (James Gosling), C# (Anders Hejlsberg)
Predecessor Language
N/A
Predecessor Appeared
N/A
Predecessor Creator
N/A
Runtime Target:
JVM
Latest Framework Target:
JDK 6
Mono Target:
No
Allows Unmanaged Code:
No
Source Code Extension:
“.ceylon”
Keywords:
35
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:
N/A
Latest IDE Support:
Eclipse
Language Reference:
Extra Info:


5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, Carlos, FYI, you can write:

    name[0..0].uppercased + name[1...]

    which is probably a little more idiomatic in this case.

    ReplyDelete
    Replies
    1. Hi Gavin,

      Excellent! that's what I was looking for but I had some problems when trying to use name[0].string.uppercased that gives a:

      member method or attribute does not exist: string in type Nothing|Character
      member method or attribute does not exist: uppercased in type unknown

      Then I tried using sequences name.characters[0].uppercased but got similar problems.

      I updated the post. Thanks.

      Carlos.

      Delete
    2. Yes, that's because name[0] is of type Character?, not String. You could write name[0]?.string?"" if you like, but I think name[0..0] or name.initial(1) is better here.

      Delete