Forums

namespace cannot contain a class with the same name

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

namespace cannot contain a class with the same name

Postby carlosqt » Thu Jul 01, 2010 3:26 pm

First post... I didnt find a way to report an issue (probably because it is already late... sleepy lol) but here we go:

If I have the following program where I define a namespace called GreetProgram and also a Class called GreetProgram I receive the compiler error shown below. In the topic of Namespaces it doesn't mention that you should not name a class the same as the namespace thats why I'm creating this post. If I change the name of the namespace or the class it works.

This program will be my next blog post: called OO Hello World in Cobra (you can have a look in http://carlosqt.blogspot.com/ )
use System
namespace GreetProgram

class Greet
var name as String

cue init(name as String)
base.init
.name = name[0].toString.toUpper + name[1:]

def salute
print "Hello [.name]!"

#Greet the world!
class GreetProgram
def main is shared
g = Greet("world")
g.salute



N:\CLR\Cobra\Cobra\Cobra\bin>cobra.exe hellocobra.cobra
n:\CLR\Cobra\Cobra\Cobra\bin\hellocobra.cobra(17): error: The type name "Greet"
does not exist in the type "GreetProgram.GreetProgram"
Compilation failed - 1 error, 0 warnings
Not running due to errors above.
Last edited by carlosqt on Thu Jul 01, 2010 11:44 pm, edited 1 time in total.
carlosqt
 
Posts: 17
Location: Belgium

Re: namespace cannot contain a class with the same name

Postby Charles » Thu Jul 01, 2010 6:58 pm

It's perfectly fine to report problems here so they can be discussed. Then we can fill out a Trac ticket if needed. The Trac wiki/ticket system login is the same as your phpBB one by the way (but still requires a separate login).

Also, if you surround your code snippet with <cobra> ... </cobra> but using square brackets instead of angles, then the code will retain its indentation. There is also a "cobra" button in the toolbar above the edit box.

Regarding your program, this is a bug in Cobra. When the back-end generates C#, it generates fully qualified references like "GreetProgram.Greet", but C# gets confused due to finding the class before it finds the namespace. I'll fix. In the mean time, you have to avoid this. I guess we don't run into this problem often because namespace names are often broad topics that contain more specific classes. For example, System.Collections contains List, Dictionary, etc.

In my own version of your program, I would leave out "use System" which is automatic and "is shared" which is also unnecessary. I might also show off extensions, unit tests and contracts, even if only a little bit:
extend String

def capitalize as String
test
assert ''.capitalize == ''
assert 'x'.capitalize == 'X'
assert 'X'.capitalize == 'X'
assert 'cobrA'.capitalize == 'CobrA'
body
branch .length
on 0, return this
on 1, return .toUpper
else, return this[0].toString.toUpper + this[1:]

class Greet

cue init(name as String)
require name.length > 0
base.init
_name = name.capitalize

get name from var as String

def salute
print 'Hello [.name]!'


class GreetProgram
""" Greet the world! """

def main
g = Greet('world')
g.salute

The String extension makes the code fatter, but you end with something you can reuse due to it being on String and having unit tests to help ensure it works.

Please feel free to blog/post whatever suits your style and goals.

Thanks for your question/report.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: namespace cannot contain a class with the same name

Postby carlosqt » Fri Jul 02, 2010 1:28 am

Hi Chuck
Thanks for the quick answer and cool you will fix it. I know is not a high priority, but I'm sure someone else will face this same issue later.

Thanks a lot for your modified sample code. It is really cool how many features the Cobra language has and I'm pretty sure I will be blogging about those in the near future, but for the moment I will keep my original program as it is to accomplish my 2 goals
1. Showing lots of languages implementing the same piece of code to be fare and not using features not available in other languages.
2. comparing the minimal and the verbose version of the same program in those languages.

For instance, comparing the same OO Hello World in Boo, IronPython and Cobra is interesting (at least for me) in many ways.

The structure of the program I'm using is:
[Import / using / etc]   <--- even if it is implicit in most .net or java languages I add it in the verbose sample
Class
Data
Constructor
Method
Program
Instantiate Class
Call Method => OUTPUT (Hello, World!)


If you are interested have a look at: http://carlosqt.blogspot.com/ :)
carlosqt
 
Posts: 17
Location: Belgium

Re: namespace cannot contain a class with the same name

Postby Charles » Fri Jul 02, 2010 6:46 am

That's cool. I look forward to seeing the Cobra post.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: namespace cannot contain a class with the same name

Postby carlosqt » Sun Jul 04, 2010 11:22 am

i almost forgot to update this post with the link lol
http://carlosqt.blogspot.com/2010/07/oo ... cobra.html
simple... if I did a mistake in the Cobra Info section, please let me know.

bytes!
carlosqt
 
Posts: 17
Location: Belgium

Re: namespace cannot contain a class with the same name

Postby Charles » Sun Jul 04, 2010 6:15 pm

Thanks, Carlos. It's nice to see the two versions which gives people more of an idea of what they can do.

-- Technically the version number is simply "2010-04-02". You probably saw "post 0.8.0" somewhere. 0.8 is an old release.

-- The indentation appears to be 1 space when it should be 4. Cobra won't accept anything other than 4 (or one tab per indent).

-- "Influenced by" should definitely include "the big four": Python, C#, Eiffel, Objective-C

Thanks!
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: namespace cannot contain a class with the same name

Postby hopscc » Mon Jul 05, 2010 3:54 am

Cobras runtime has a String extension method called capitalized which does the same as the
slicing and toUpper line (similar to many of the other language examples)
Code: Select all
   .name = name[0].toString.toUpper + name[1:]
   # becomes
   .name = name.capitalized
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: namespace cannot contain a class with the same name

Postby carlosqt » Fri Oct 22, 2010 1:45 am

Thanks for the comments, and sorry for the delay. I updated the post.
I'm working on my next post about Cobra (Basics by Example) which will give a more elaborated example using basic OO constructs.
If you are wondering why 3 months after is because I'm doing the same for other 19 languages which most of the time I learn while writing about them lol such as with Cobra.

Concerning the Indent I posted a note before the code so people be aware before trying the code.

Thanks for the Capitalized clarification. I added it as well.
carlosqt
 
Posts: 17
Location: Belgium

Re: namespace cannot contain a class with the same name

Postby Charles » Fri Oct 22, 2010 11:41 am

Carlos, thanks for the update and for covering Cobra in your series on languages.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: namespace cannot contain a class with the same name

Postby carlosqt » Sat Oct 23, 2010 4:21 am

You are welcome,
Really like Cobra language syntax and features set

Post done: http://carlosqt.blogspot.com/2010/10/cobra-basics-by-example.html

if you find any error let me know :)
carlosqt
 
Posts: 17
Location: Belgium

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 17 guests

cron