Posts Tagged ‘programming language’
Cobra — An alternative?
As probably widely known, my preferred programming language is Python. But … well … I like to try out new things. Thus I sometimes look through the interwebs to find an interesting new alternative (Note: I still want to learn (and use) Haskell…). And recently I found Cobra: In short: It has a pythonic syntax, but knows natively about unit-tests and design by contract. The latter point is the interesting one: I also wanted to try D for the same purpose, but it a) has no real open source compiler (I know of), and b) pythonic look and feel is better then C-like. Only drawback(?): It is .NET based. But well … we’ll see how this works out. Cobra itself is also designed in a way, that it can use different backends later on. (I like exchangable backends :P)
The features of Cobra:
- Static and dynamic typing
- Fast(er) execution (due to using .NET)
- Pythonic … therefore quite quick to program in
- Language level support for quality
Lets put some code here (taken from the HowTo directory :))
class Utils
shared
def compute(x as float, y as float) as float
require
x > 0
y > 0
ensure
result > 0
body
return x.sqrt + y.sqrt
def normalizeName(name as String) as String
# If a contract has only one condition, you can write it
# on one line and skip 'body'.
require name.trim.length
ensure result.length <= name.length
return Utils.capped(name.trim)
def capped(s as String) as String
"""
Returns the string with the first character capitalized.
Returns a blank string for a blank string.
"""
ensure
result.length == s.length
result.length implies result[0] == result[0].toUpper
test
# Unit tests can enhance contracts by showing example
# invocations and verifying behavior.
assert Utils.capped('chuck')=='Chuck'
assert Utils.capped('Chuck')=='Chuck'
assert Utils.capped('')==''
assert Utils.capped(' foo')==' foo'
assert Utils.capped('f')=='F'
assert Utils.capped('1aoeu')=='1aoeu'
body
if s.length
return s[0:1].toUpper + s[1:]
else
return s
So … let’s see where this is going to lead :). I plan to make an ebuild for the next release (the current ones do not include sourcecode) … and then the fun can start.