Page 1 of 1

hi

PostPosted: Wed Oct 06, 2010 9:39 pm
by soham
Hi all,

I am a newbie to cobra and am having some problem in understanding some of the stuff here.

In the introductory codes that are provided like class Fib where Fibonacci series is calculated till tenth term. The loop is as follows

for i in n

n has been initialised to 10 but i is declared first time and not even initialised! I have had experience with python but then too the code looks something like this for i in range(n). Although I have seen dynamic typing but I feel kind of extreme.

How does i iterate over n ? I mean first of all how does i initialise to zero and then the loop looks like a typical "for each" rather than for loop so what type is 'i' ? Is it a iterator ? I mean is n cast to some sort of enumerable thing and the compiler makes a list of it and then you iterate over that range or something else.

I am sorry if I am bugging everyone else by over thinking or complicating this stuff but I am curious to know what is happening behind the hood.

Some of my view points
Does forcing the opening brace "(" right after method definition name have any advantage ? I program in some other langauges where I use space liberally that is method name space opening and closing parentheses. Just a minor issue but felt sort of awkward. Similar for the case when specifying the List<T> generic thing this being not valid List< T > (notice the space between arguments and bracket) unlike Java or C#. Funny thing is cobra allows me to put space between parentheses and argument names in between them.

No incremence or decremence operator. I do not get it completly but is there any advantage in throwing out ++ and -- operators.

What does keyword is shared means and are methods in cobra static as like in Java since in the program the method is called as Fib.compute() that is class name and dot operator method name. Or is it some sort of access modifier like public, private, protected.

Lastly the following code refused to run on my machine though I copied it word to word from site

class Fib inherits List<of int>

def init( count as int )
base.init( count )
a, b = 0, 1
for i in count
.add(b)
save = a
a = b
b += save

class Program

def main is shared
i = 1
for n in Fib.( 10 )
print '[i]. [ n ]'
i += 1

It just said
error: COBRA INTERNAL ERROR / InvalidCastException / Cannot cast from source type to destination type.
Compilation failed - 1 error, 0 warnings

regards,
Soham

Re: hi

PostPosted: Thu Oct 07, 2010 1:41 am
by Charles
-- The loop "for i in n" is known an the "numeric for loop". Its general form is "for VAR in EXPR" where EXPR is a numeric expression. It is simply syntactic sugar. In a C-derived language, it would be "for (int i = 0; i < n; i++)". Compared to Java and C#, Cobra places more emphasis on clean code. You can control the starting value too. See NumericFor.

-- Some of the syntactic restrictions, like those around passing arguments to methods, are designed to make code bases more uniform between projects. One benefit is that once you get used to the syntax, it is quicker to read other people's code. It's also easier to inherit other code bases. There is also less dispute over style guidelines because there is less to dispute.

-- Like Python, Cobra uses augmented assignments for increment and decrement such as "x += 1" and "x -= 1". The ++ and -- in C let you get fancier with pre-increment vs post-increment, but it can make expressions more difficult to read and hardly seems necessary in practice.

-- "shared" in Cobra is "static" in C# and Java and "Shared" in Visual Basic.

-- Sorry to hear about your compilation error. In the future, you will want to share your op sys name and version, and Cobra version. In this case, I can reproduce the problem. Change this line:
for n in Fib.( 10 )
to this line
for n in Fib( 10 )
In other words, remove the period (".") after "Fib" and before "(".

I will fix the bug where the compiler chokes on this syntax error.

Thanks for your interest in Cobra.

Re: hi

PostPosted: Thu Oct 07, 2010 6:16 am
by soham
Thanks Chuck,

I got it. Sorry for the Fib.(10) thing I must be getting blind or should have got that one myself. By the way I am using Cobra version released on 2010-04-02 on Ubuntu 10.04.

Another question on the risk of being sounding rude but just curious. I found Cobra to be a bit slow when I was trying to compile from unix command line. Unlike go ( Google's programming language) which is freaking fast; for a new language and that is compiled and has static typing I expected it to be as fast as go or C++. Even for a mickey mouse program like hello world kind of it took close to a sec or so before compilation success message. Even Java produces intermediate code but found Java faster in this case. This is just a rough observation based on few programs not on a huge bunch of programs.

Is this slowness because I am executing on Ubuntu or because it is generating intermediate byte code or am I comparing comparing chalk and cheese or seeing things that are not there.

regards,
Soham

Re: hi

PostPosted: Thu Oct 07, 2010 7:34 am
by soham
There is a new hic cup now.

class Fib inherits List<of int>

def init( count as int )
base.init( count )
a, b = 0, 1
for i in count
.add(b)
save = a
a = b
b += save

class Program

def main is shared
i = 1
for n in Fib( 10 )
print '[i]. [ n ]'
i += 1

This time the compiler yells
hello.cobra(4): error: Cannot find a definition for "init" in "base" whose type is "Fib". There are members with similar names including ".init" and ".cue.init".
hello.cobra(3): warning: Did you mean "cue init"?
Compilation failed - 1 error, 1 warning
Not running due to errors above.

I am using Ubuntu 10.04 and version released on 2010-04-02

regards,
Soham

Re: hi

PostPosted: Thu Oct 07, 2010 10:06 am
by Charles
-- When you post code, please surround it with <cobra> and </cobra> but with square brackets instead of angle. That will preserve the indentation.

-- You should write "cue init" instead of "def init". Did you see the warning "hello.cobra(3): warning: Did you mean "cue init"?" It was meant to point you in that direction. We try to give hints like that in the errors and warnings, especially to help newcomers.

-- I agree that the compiler is slow. There are multiple reasons for this and I think we can improve it over time.

Thanks for sharing your observations and questions.

Re: hi

PostPosted: Thu Oct 07, 2010 1:51 pm
by soham
Thanks Chuck,

That worked out well. I will go through more of the documentation to understand what is going on. Thanks for the help again.

regards,
Soham

Re: hi

PostPosted: Fri Oct 08, 2010 7:33 am
by Charles
No problem.

Re: hi

PostPosted: Mon Oct 11, 2010 10:17 pm
by Charles
I have fixed the problem where "Foo.(5)" caused an internal compiler exception rather than giving a 'Syntax error after "."' message. Thanks for reporting this.

Re: hi

PostPosted: Thu Dec 16, 2010 1:36 pm
by johannes.krampf
The last example on the introduction page still has the error described by soham above. Changing def init to cue init does indeed fix the problem, but I think you should have beginners who are still learning the basics of Cobra solve riddles.

Would you kindly change the example?