For now, let's build them up in this thread. Feel free to add as you discover things not described here.
First, be sure to study
http://cobra-language.com/how-to/ which includes code dedicated to teaching the syntax.
I'll add to your comments:
- Code: Select all
// C#
DateTime start = DateTime.Now;
...
TimeSpan duration = DateTime.Now - start;
start = DateTime.now
...
duration = DateTime.now.subtract(start)
I'm in favor of operator overloading, but Cobra doesn't have that yet, so you have to make use of the "add" and "substract" methods found in various library classes.
As you can see, Cobra can infer the type and requires no keyword to declare a local.
Also, comments are done with # sign instead of //.
"does not equal" in Cobra is "<>" not "!=".
You
can overload all comparison operators in one fell swoop like so:
class Value
var _x as int
def init(x as int)
_x = x
get x from var
def compareTo(other as Value) as int
return _x - other.x
class Program
def main is shared
a = Value(1)
b = Value(2)
assert b > a
assert b >= a
assert a < b
assert not a > b
Also you may have noticed that constructors are done with "def init".
There are no multi-line comments. A common technique to disable code is to put an "if false" above it and indent the subsequent code.
You can do this in Cobra:
if x > y
max = x
else
max = y
print max
So the "max" variable is accessible after its declaration. That's a poor example as there is also an if() expression, equivalent to C#'s ?:, that would be more appropriate here:
max = if(x > y, x, y)
print max
Cobra supports +=, -=, etc. It does not support ++ or -- unary operators so use += 1 and -= 1 instead.
Numeric for loops look like this:
for x = 0 .. 10
print x
for x = 0 .. 100 ++ 2
print x
for x = 100 .. 0 -- 1
print x
# a more realistic use:
for i = 0 .. someList.count
someList[i ] = someList[i ].transform
So here you can see the ++ and -- tokens are used as separators "for VAR = START .. STOP ++ STEP". Also, loops are left inclusive and right exclusive which matches the valid indexes on lists and arrays.
Typecasting is done with "EXPR to TYPE" as in "x to int". In C# that would be "(int)x".
Statements that require "truth" such as "if" and "while" will accept
any type. For example, a non-zero integer is considered true:
if items.count
print 'I have stuff'
You can do string literals with single or double quotes. I like single myself.
You can substitute values directly in strings with square brackets:
print 'Hi, [name]. How are you?'
That's good for now.