Forums

What am I doing wrong?

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

What am I doing wrong?

Postby lgastako » Thu Nov 11, 2010 6:37 pm

Hi all,

I'm trying to implement skip lists in Cobra as a means of exploring/learning the language. I started by implementing the basic interface I wanted wrapped around a regular list and now I am slowing converting it over to actually be a skip list... unfortunately I ran into some errors that I don't understand pretty quickly:

$ cobra skiplist.cobra 
skiplist.cobra(113): error: Cannot return IComparable because "itemAt" is declared to return a IComparable.
skiplist.cobra(176): error: Argument 1 of method "indexOf" expects type IComparable, but the call is supplying type IComparable.
skiplist.cobra(179): error: Argument 1 of method "find" expects type IComparable, but the call is supplying type IComparable.
skiplist.cobra(182): error: Cannot return List<of IComparable> because "toList" is declared to return a List<of IComparable>.
Compilation failed - 4 errors, 0 warnings
Not running due to errors above.


The source code is here: https://github.com/lgastako/CobraSkipLi ... list.cobra

I don't expect the code to work yet but I don't understand why it would fail in this particular way -- in fact the error messages are confusing for obvious reasons -- in each case it seems to be saying "I wanted an X but you gave me an X" where X = X.

What am I missing?

Thanks.

P.S. Cobra is awesome.
lgastako
 
Posts: 2

Re: What am I doing wrong?

Postby Charles » Thu Nov 11, 2010 9:10 pm

I don't think you're missing anything. Looks like a bug with me. Probably generics. I am always happy to squash these. I'll post back when it's fixed.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: What am I doing wrong?

Postby Charles » Thu Nov 11, 2010 10:10 pm

Well I should have seen this sooner, but where you have:
class SkipList<of IComparable>
It should be:
class SkipList<of T>
where T must be IComparable
The identifiers inside a generic declaration such as:
class Something<of T>
are newly declared type parameters which you'll then use in your subsequent code. If you want to constrain them to one or more interfaces, add a "where ... must be ..." clause.

You can browse the test cases for generics at CobraWorkspace/Tests/240-generics or online.

By the way, if you want a longer type name, there is a convention of prefixing generic params with a capital T such as "TComparable" or "TShape".

So you did do something wrong after all, but this leaves me wondering if a new warning or error message is called for. It seems that when a declared generic parameter is named the same as an outer type it is likely to be a mistake.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: What am I doing wrong?

Postby Charles » Thu Nov 11, 2010 10:23 pm

Well I'm at least improving the error message (and others) to be:

error: Cannot return interface "IComparable" because "itemAt" is declared to return a generic parameter "IComparable".

Not checked in yet.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 80 guests