Page 2 of 2

Re: (Mostly) operator overloading & autoboxing

PostPosted: Sun Mar 30, 2008 1:38 pm
by Charles
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.

Re: (Mostly) operator overloading & autoboxing

PostPosted: Sun Mar 30, 2008 2:09 pm
by Charles
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

Re: (Mostly) operator overloading & autoboxing

PostPosted: Sun Mar 30, 2008 2:29 pm
by themaniac
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.

Re: (Mostly) operator overloading & autoboxing

PostPosted: Sun Mar 30, 2008 4:28 pm
by Charles
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?

Re: (Mostly) operator overloading & autoboxing

PostPosted: Mon Mar 31, 2008 4:19 pm
by themaniac
'Course - permission granted