Forums

(Mostly) operator overloading & autoboxing

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

Re: (Mostly) operator overloading & autoboxing

Postby Charles » Sun Mar 30, 2008 1:38 pm

I already fixed that problem for single line comments, but apparently not for doc strings. I'm in the middle of something, but I'll post again when this is fixed.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: (Mostly) operator overloading & autoboxing

Postby Charles » Sun Mar 30, 2008 2:09 pm

Actually this is working fine. After the indentation you can freely mix tabs and spaces. But doc strings are expected to maintain the same level of indentation as where they started. This is where Cobra is complaining about the mixing of tabs and spaces. For now anyway, I'm leaving this.

A few comments on the code:

-- You can use a "post while <condition>" loop if you like. This is like the "do ... while <condition>" in pseudo-code.

-- You might wish to put "shared" after "class Sorter" and indent the methods by one level since Sorter is not maintaining any state.

-- Since all the expectations of the sort method results are the same, you can move that out to a test method that takes the sort method as an argument. I've tested that this code works:
class Sorter

sig SortMethod(list as IList) as dynamic

shared

def testSort(sortMethod as SortMethod)
assert sortMethod([1,2,3,4])==[1,2,3,4]
assert sortMethod([3,4,1,2])==[1,2,3,4]
assert sortMethod([4,3,2,1])==[1,2,3,4]
assert sortMethod(['c','d','b','a'])==['a','b','c','d']
assert sortMethod([1,1,1,1])==[1,1,1,1]

def bubblesort(list as IList) as dynamic
test
.testSort(ref .bubblesort)
body
swapped=true
while swapped
swapped=false
for i = 0 .. list.count - 1
if list[i] to dynamic > list[i+1] to dynamic
.swap(list, i, i+1)
swapped=true
return list

def swap(list, a as int, b as int)
tmp = list[b]
list[b]=list[a]
list[a]=tmp

def main is shared
pass

-- It might be interesting to generate a really large list and run each of the algorithms, testing them for speed. But I would save that for a "phase 2" and just get the current stuff working.

HTH,
-Chuck
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: (Mostly) operator overloading & autoboxing

Postby themaniac » Sun Mar 30, 2008 2:29 pm

Thanks. Please find an updated version attached. I think you can ignore what I was saying about the docstring indentation - turns out I was missing a """

But I have found another potential source of confusion:
Code: Select all
class TestStatic
   shared
      def main is shared
         pass

gives
Code: Select all
TestStatic.cobra(7): error: Duplicate "static" modifier

I presume it should say Duplicate 'shared' modifier, but I presume that it says static because the problem is only caught at the c# compilation stage.
Attachments
102-ImplementSorting.cobra
Updated version
(5.91 KiB) Downloaded 594 times
themaniac
 
Posts: 28

Re: (Mostly) operator overloading & autoboxing

Postby Charles » Sun Mar 30, 2008 4:28 pm

Yeah, I'll fix.

Do I have your permission to add this to the How To and the distribution? Also, do you want a credit in the doc string?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: (Mostly) operator overloading & autoboxing

Postby themaniac » Mon Mar 31, 2008 4:19 pm

'Course - permission granted
themaniac
 
Posts: 28

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 29 guests