Forums

Understanding Cobra

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

Understanding Cobra

Postby katane » Fri Mar 07, 2014 12:33 am

Hi, I'm a novice programmer. I learned most of my programming from Ada 2005 book. I know an intermediate level of Ada.

I'm trying to find more samples and instructions on how Cobra works. I noticed its more of text based UI. My question is, are there any programming languages I need to get on basis to learn more of Cobra? If so, which ones do you prefer?

This is my first attempt of Cobra, trying to mess with syntax and such. I know there may be huge errors in this code block, but this is my experience from reading the How To and Samples.

Code: Select all
   class newObject
   
   var _object = Local(1, 2)
      assert 1a.x == 1 and l.y == 2
      assert 1a.isPositive and not 1a.isZero
      assert 1a.toString == 'Object (1, 2)'
      def localNumber(num as int) as int

   _newObject = o2.raisedBy(3, 4)
      assert o2.x == 4 and o2.y == 6
      assert _object <> _newObject
      
   _savedObject = Local(1, 2)
      assert _object == _savedObject
      
      assert point.zero.isZero
      
      Locals = { _newObject, _object,
               _savedObject}
               
      assert locals.count = 2
      assert _object in locals and _newObject in
         locals and _savedObject in locals
         
            shared
   
   var _zero = Local(0, 0)
      get zero as Local
            return _zero
            
            cue init(x as int, y as int)
               ensure .x == x and .y == y
            _x, _y = x, y
      get x from var as int
      get y from var as int
            get isPositive as bool
            return .x > 0 and .y > 0
         get isZero as bool
            return .x == 0 and .y == 0
            
      def raisedBy (2 as int, 5 as int) as Local
         ensure result.x == .x+2 and result.y == .y+5
            return Local(_x.2, _y.5)
            
            def toString as String is override
         return '[.newObject.is]([.x], [.y])'
         
         def equals(isZero as Object?) as bool is override
            if isPositive is isZero, return true
            if isZero inherits Local
               return .x == isZero.x and .y == isZero.y
            else
               return false
               
               def line as int is override
                  return .x ^ .y



Seeing from the above, self teaching myself programming is a little hard my main objective was to exclude a point that was zero as a saved var.
katane
 
Posts: 2

Re: Understanding Cobra

Postby nerdzero » Sat Mar 08, 2014 4:52 pm

Hi katane,

katane wrote:My question is, are there any programming languages I need to get on basis to learn more of Cobra? If so, which ones do you prefer?

Cobra has been influenced by a lot of languages. You can read more about that here: http://cobra-language.com/docs/why/

Cobra has "significant whitespace" which means that the amount that you indent your code with tabs or spaces affects the compilation of your program. This exists in many other languages but the Python language is probably the most well known for this. Cobra syntax is also influenced by Python in a number of other ways so being familiar with Python can help when learning Cobra.

Being familiar with C# or VB can also help because Cobra compiles to C#. That means if you know the .NET standard library well, you don't spend a lot of time reinventing things that are already available and ready to use.

Also, there are more beginner-level tutorials available for Python and C#, so starting with one of these might be an easier route for a novice than starting with Cobra directly.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Understanding Cobra

Postby katane » Sun Mar 09, 2014 7:45 pm

nerdzero wrote:
Cobra has "significant whitespace" ...


Can you create your own syntax "start blocks" and "end blocks" using Cobra while obeying the significant whitespace parent and child rule?
katane
 
Posts: 2

Re: Understanding Cobra

Postby hopscc » Sun Mar 09, 2014 10:27 pm

Theres no arbitrary code block construct
like
Code: Select all
    if ( a > 10) {
        printf("a=%d\n", a);
    }
   
    // arbitrary block
    {
        int b = 99;
        printf("b=%d\n", b);
    }
    /*  printf("b=%d\n", b);  // syntax error */

if thats what you mean ( though you can fake one);
This is less useful/needful in cobra since local vars arent local scoped to the block they are dcled/first used in.... pushed out to enclosing
method block

def aMethd
# (0)
if true
j = 99 # variable first used/appears to come into existence here
print j
print j # also in existence here ( outside 'first-use' block)

Actual declaration happens implicitly at start of method ( at #(0) )

The code blocks are built into each statement that allows a block of code
# e.g if block
if isBlue
doBlue1
doBlue2( thing)
doneBlue +=1

print 'allDone'

# method code block
def aMethod
i =99
for j in 0 : i # for block follows
print 'j = [j]'
print '.'
print 'Done

if true
print 'in a block'
'

See some code examples samples and How To
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Understanding Cobra

Postby kobi7 » Mon Mar 10, 2014 10:29 am

Hello Katane, and welcome!

I came to cobra from C#, (and have some experience with more esoteric languages.)
knowing csharp is not a requirement.
Cobra doesn't have a lot of libraries, but the advantage here is that cobra targets the .net runtime.
so everything there is in .NET you can use in your programs. the whole .net ecosystem.
here is the full list of classes:
http://msdn.microsoft.com/en-us/library ... 00%29.aspx
searching in google for "msdn csharp <yourclass>" will usually get you to the correct place. for example "msdn csharp bitmap"

Aside from creating standalone programs in Cobra, if your project is a cobra library, you will be able to use the resulting dll in C# or other popular languages that target .net and the library's api will look fine to other members in the team in terms of convention and style.
so you can use the friendly "secret weapon", while interoperating with others.

I recommend you take a look at this page, for quick introduction of what you can use:
http://cobra-language.com/trac/cobra/wi ... axHilights
the reference manual:
http://cobra-language.com/trac/cobra/wi ... uageTopics
and the built in libraries (especially extension methods)
http://cobra-language.com/trac/cobra/wiki/LibraryTopics

Try something small and see how it feels for you
I find that it's clean, concise, has .net, and you don't waste whole days hunting bugs thanks to built-in contracts (design by contract).

I also recommend working with the command line compiler at the beginning so you know how to use it, and what options you have in your disposal. (for example testing)
There is a MonoDevelop addin, that saves much time when you write code since it has autocompletion - it's easier to discover the method names in classes. (you're not supposed to remember the entire .net framework!)

see you, kobi
Falun Dafa is Good.
Truth, Compassion, Forbearance is Good.
kobi7
 
Posts: 82
Location: Israel


Return to Discussion

Who is online

Users browsing this forum: No registered users and 23 guests