= Cobra Differences From C# = == Illustrated == {{{ #!cobra # Cobra d = {'a': 1, 'b': 2} assert d['a'] == 1 and d['b'] == 2 for key, value in d print '[key] = [value]' # string interpolation }}} {{{ // C# var dictionary = new Dictionary { {"a", 1}, {"b", 2} }; Assert.IsTrue(dictionary["a"] == 1 && dictionary["b"] == 2); foreach (var key in dictionary.Keys) { var value = dictionary[key]; Debug.WriteLine(string.Format("{0} == {1}", key, value)); } }}} credit: http://fir3pho3nixx.blogspot.com/2011/01/when-c-is-not-enough.html == Quick Reference == * Dynamic Typing and Type inference (doesnt require Type or keyword to declare locals) * optional type declarations * Block structured by indentation only (like Python) no squiggly braces (brackets) * nil tracking - variables can be declared as nil assignable (or not) * Built in language support for * unit test code * assertions * Design by contract - invariants, preconditions and postconditions * Syntax support for builtin * Lists - [] - List of Dynamic * Dictionaries - {:} Dictionary of * Sets - {,} * Line Oriented * '''#''' to comment line (instead of //) * '''/# ... #/''' for multiline comments * Line continuation is implied in method argument lists: foo.bar(arg1,[NEWLINE]arg2) * Use a trailing '''_''' for explicit line continuation * Tab or 4 Spaces to indent line (Fixed 4 space indent), Cannot mix Tabs and spaces for an indent level * No statement terminator (goodbye to ';') * Fractional numbers default to decimal type (rather than int)- can be overridden (to float/double) with a compiler commandline option * Method names and properties start with lowercase. (i.e lowerCamelCase) * Access to methods/properties in used libraries are also done via (implicitly mapped) lowerCamelCase methodnames * In resulting binaries such as .exe and .dll, the .NET standard CamelCase is used so that Cobra libraries vend out naturally to C# and VB.NET * Class names must be (upper) [http://en.wikipedia.org/wiki/CamelCase CamelCased] or at least start with an upper case letter) * instance variable optionally prefixed with _ (implicitly makes them protected) or __ (implicitly makes them private. If so prefixed can be accessed directly in methods (without a leading '.') * methods (and properties) on current object invoked with leading '''this.''' or more usually (shortcut) just '''.'''. * constructor/initializer method is called init, has no return value * String type accessed as '''String''' * Numeric types specified as int, uint, float/float64, float32 and decimal * Sized int and uint types all have the same naming form * int8, int16, int/int32, int64 instead of SByte, Int16, Int32/int, Int64 * uint8, uint16, uint/uint32, uint64 instead of Byte, UInt16, ... * float32, float/float64 instead of float, double * Type of numeric literals can be dictated using suffixes * Variable specification - typing, access modifiers, argument attributes specified by keyword '''as''' rather than C-like/Java-like syntax * e.g. x as int32, instVar as string public (vs int32 x; public String instVar; ) * typecasting by keyword '''to''' as in 'x to Type' rather than '(Type)x' * cast to and away from nilable Type. * '''is shared''' to declare a static type * '''<>''' instead of '''!=''' for 'not equals' comparisons * Condition negation uses keyword '''not''' ( as in {{{ not a >10}}} ) * boolean conditions use keywords '''and''' and '''or''' ( as in {{{ if a>3 and b<10 or b>55 }}}) * boolean '''and''' and '''or''' have equal precedence in Cobra whereas in C# they are differentiated. When converting code to Cobra, additional parentheses may be needed. * '''nil''' for no object (null) value * Supports +=, -=, etc. Does not support ++ or -- unary operators ( use += 1 and -= 1 instead). * boolean conditions can be tested on any type. True is any of * non false (bool Type) * numeric not 0 * non zero char * not nil * blank strings and empty collections are true (non nil) - must check .length for strings and .count for collections * Strings delimited by single or double quotes, normal range of \c expansion ( e.g \n, \t) * Substitute expressions (values) directly in strings by enclosing with []. * such expressions can have optional String.format specification * 'Raw' Strings (r"a raw string") no escape sequence expansion ( vs c# @"a raw string") * '!NoSubstitution' strings ( ns" no substitution [done] here") * !DocStrings on Programs, Classes, Interfaces (etc), instance variables, methods and properties * Different syntax for characters c'x' * since '[] used for declaring literal Lists, array literals are specified using ''' @[]''' - comma separated values * '''ref''' keyword for getting a reference (e.g., safe pointer) to a method, rather than calling it, so the reference can be stored or passed around. (for delegates) * '''sig''' keyword for declaring a delegate type * '''def''' keyword to declare a method/function * e.g def doCalc( start as int) as int instead of int doCalc( int start) * '''use''' keyword instead of 'using' to get access to a non default namespace * ''' branch... on, on, [else]''' statement rather than c# '''case''' statement * Slicing syntax for sequences (Lists and Strings) [start:stop:step] as well as normal library subsequence methods (substring, range, ...) * numeric for loop uses slice syntax. e.g. for i in 1:10:1 vs for( int i =1; i<=10; i++) * the Cobra ternary operator has a different syntax: {{{if(, , )}}}. Example at http://cobra-language.com/docs/manual/expressions/if.html * more as they surface == See also == * [http://cobra-language.com/forums/viewtopic.php?f=4&t=15 Cobra Forums Differences with C#] * http://cobra-language.com/how-to/