Forums

Differences with C#

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Differences with C#

Postby neuruss » Fri Feb 08, 2008 9:01 am

I wonder if someone could write a brief description of differences between Cobra and C#.
So far I've been bitten by small details such capital letters in method names, or "shared" instead static, etc...

There is a comparison between python and Cobra, but since we are in a .NET environment, I guess it would be more useful to know the syntactic and philosophical differences with other .NET languages.

neuruss
neuruss
 
Posts: 23

Re: Differences with C#

Postby Charles » Fri Feb 08, 2008 12:13 pm

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. :D
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Differences with C#

Postby RichS » Fri Feb 08, 2008 12:56 pm

Are functions (methods) first class objects?

I couldn't find it the docs, and my attempts to assign them to variables have thus far failed.
RichS
 
Posts: 8

Re: Differences with C#

Postby Charles » Fri Feb 08, 2008 1:08 pm

They will be.

.NET calls these delegates, but Cobra does not yet support them so you cannot pass them around. Well you can for events:
listen saveButton.onClick, ref .save

The word "ref" indicates that you are referring to "save", not invoking it. This allows the extremely frequent case of invoking a method to by syntactically clean (example: "obj.getType.baseType.name"). It also means that as soon as you see the "ref" in "ref obj.foo.bar", you know what's going on. In C#, the meaning of "obj.foo.bar" depends on whether "bar" is a property (semantics = invoke) or a method (semantics = reference).

I realize how important it is to pass around delegates and so it's a very high priority. In the mean time, I get by with subclassing and overriding methods.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Differences with C#

Postby neuruss » Fri Feb 08, 2008 1:48 pm

I see that the starting point of a program is always the main method of a class.
Is this going to be always that way, like in c# or Java ?
If so, why? Why not like in python or Boo?
Seeing the emphasis in clear code in Cobra, this looks odd...
neuruss
 
Posts: 23

Re: Differences with C#

Postby Charles » Fri Feb 08, 2008 1:58 pm

Changing the language to support:
def main
print 'Hello'

Only saves you from 1 line of code and "is shared":
class Program
def main is shared

Also it's a fairly rare event: once per program. Compared to all the other code you write for a program, it's trivial overhead.

Cobra is still cleaner than the octuple underscore approach in Python ("if __name___=='__main__': ...") which is also two statements of overhead:
Code: Select all
# Python
def main():
    print 'hello'
# recommended technique:
if __name__ == '__main__':
    main()
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 20 guests