A glance at ooc
A breath of fresh OO programming
Ooc is an object oriented programming language that aims at efficiency and readability. One of ooc’s great stength is that the code is converted into C and then compiled with gcc. Yes, you have read correctly, ooc is a OO language with garbage collector and all nice features a modern language can offer that is translated into old, fast, well known C. We will see what advantages this gives us … speed for sure !
Readability is one key of maintenability. The syntax has been thought to be as clear and concise as possible applying the Don’t Repeat Yourself (DRY) moto. Only the important code deserves being visible.
Plug and play !
Installing ooc is as easy as snapping fingers !
git clone http://github.com/nddrylliog/rock.git
cd rock && make rescue
For the little story, rock is the ooc compiler written in ooc. This means you need a working ooc compiler to compile ooc … the make rescue will download a compiled compiler to compile your compiler. One everything compiled you should find an executable binary named «rock» gently lying in the bin directory.
bin/rock --version
rock 0.9.2-head, built on 2010-10-05 at 16:46
Hello world
Edit a file hello_world.ooc :
"Hello world !" println()
You can see here the string “Hello World !” is automaticaly treated as a String object so you can invoke its method println() . I can see your incredulous eyes getting bigger as you understand that, in ooc, the operator for class belonging is not the dot as it is in Java, Python, Ruby, Javascript but the space. This makes your code to look like normal sentences.
save your file and compile it with rock :
bin/rock -r hello_world.ooc
[lot of warning messages]
Hello World !
In fact, the code above is equivalent to :
main: func
{
"Hello World !" println()
}
OOC : Outsmarting syntax
Ooc has a unique way of declaring things. Let’s see some exemples :
dog: class extends animal { tatoo_number: static constant "190832-112-AS" age: Int color: Stringbark: func { "Woof" println() } }
Every declaration is always like «name: what-it-is». From this point of view, a class method is not different from an attribute, it just contains a function object. Of course, because ooc lets you write concise code, you can shorten long variables declarations :
a: Int
b: Int
c: Int
// is equivalent to
a, b, c: Int
If you are not used to typed langagues, you may find hassling to have to write
a: Int
a = 2
// This is equivalent to
a := 2
Wow ! Here, ooc directly infers 2 is an Int instance with value 2. The := operator is a declaration AND an assignment in the same time. Function declarations are also full of handy shortcuts :
// plop is a function that does nothing plop: func// plop is a function that returns "plop" plop: func -> String { return "plop" }// which is equivalent to plop: func -> String { "plop" }// a function returns the last evaluated expression unless you specify the value to be returned // Let's try passing argumentsadd: func (a, b: Int) -> Int { a + b }
And, of course you have shortcuts for getter, setters definitions :
Human: class { age: Int name: Stringinit: func (age: Int, name: String) { this age = age this name = name } }
How boring is that ? Wouldn’t be too nice if there were a simple way to tell the same thing ?
init: func (.age, .name)
{
this age = age
this name = name
}
Oh well, we already knew ooc could infer data types, but it can do better :
init: func (=age, =name)
One last feature : overloading. There is a trick here, because overloading is about declaring several times the same function with a different number of arguments, we have to tell the compiler the function’s name in C is going to be different. We add a suffix information with a tilde to ensure there won’t be naming collision in the C source.
FiboResult: class { a, b, ttl: Intinit: func { a = b = 1 ttl = 0 }launch: func ~start (=ttl) { launch() }launch: func { temp := b b = a + b a = temp ttl = ttl - 1 "%d" printfln(a)if ( ttl > 0) { launch() } } }main: func { fibo := FiboResult new() .launch(30) }
In this exemple we can spot at line 33 the .lauch(30). Why is there this dot before the method call ? This is yet another trick to enhance visibility. The new() method does not return anything this means we cannot chain anything after it.
This code
fibo: FiboResult
fibo = FiboResult new()
fibo lauch(30)
and this code
fibo := FiboResult new() .launch(30)
Are equivalent.
In ooc, there is C
If, like me, you spent the * last years coding in PHP you might be frustrated of using always the same data structures all the time : HashMaps ! Ooc provides a lot of different data structures so you can solve problems efficientely with minimum resources consumption. Take a look at your rock directory you will see a sdk subdirectory which contains a lot of interesting resources. The structs subdirectory contains the following files :
ArrayList.ooc
Bag.ooc
HashBag.ooc
HashMap.ooc
LinkedList.ooc
List.ooc
MultiMap.ooc
OrderedMultiMap.ooc
Stack.ooc
Stick.ooc
Here are the different structures from the stacks to the linked lists and bags. Using one of these structures is as easy as adding :
// I want to use stacks
import structs/Stack
on top of your source file.
As we said earlier, functions are also objects. They can be passed as arguments to other functions. If you are a Ruby programmer you are already used to this syntax :
import structs/HashMapages := HashMap<String, Int> new()ages["arthur"] = 2 ages["john"] = 12 ages["peter"] = 23 ages["sarah"] = 24ages each(|name, age| "%s is %d years old." printfln(name toCString(), age))
the call to «toCString» method can be a bit confusing. Strings used to be a char* in the first versions of ooc. Now there is a class for that and Strings need to be converted when using native functions.
Another tremendous feature of ooc is «covers». Covers let you create classes that are mapped on existing C or C++ libs. Take a look at ooc-curl on github. It’s almost one file that maps an ooc Curl class in the existing libcurl library. In other words you are one cover away from thousands existing libraries in C and C++.
Hope you enjoyed our little tour of this shiny, sexy, young and powerful language. I hope you will be as pleased as I am learning it. Once you will begin to have consequent programmes written in ooc, you will thanks the development team to make you able to debug directly the ooc code using gdb.