(Mostly) operator overloading & autoboxing
Posted: Wed Mar 26, 2008 6:02 pm
One of the first things I try doing to see whether I like a language for writing algorithms in is see what a toy quicksort looks like.
Here's my effort (please ignore whether or not I've missed something in the implementation i.e. whether or not it will actually sort anything!)
Which doesn't work, for several reasons (perhaps it would have been more helpful if I'd posted them in a less long-winded manner, but such is life).
The issues are therefore
a) If dictionaries, sets and lists are to be first class syntactic objects (i.e. {:}, {} and []) then I'd really like to be able to perform first class algebra on them.
Simplistically therefore
The other operators are more debatable, but it might be nice to use - for exclusion on Sets (and perhaps \ for intersection?)
We'd also need to specify that + on a list is equivalent to addrange rather than add (i.e. [1]+[2,3]=[1,2,3], not [1,[2,3]]) and is only meaningful for compatible types. Perhaps these are insufficiently obvious to be allowable (after all, that's why people hate operator overloading in the first place) making this a bad idea. At any rate, I do know I want + for Lists
b) I started off trying to make my list a List<of IComparable>, but then it wouldn't accept ints, so I relaxed that to dynamic, but it *still* won't accept ints (I'm assuming because an int is not an Object). (I should say at this point I come from a Java background not a C# background). I realise it's pretty sophisticated, but are there plans to add autoboxing so that this works? (i.e. I think the new versions of Java (1.5 and onwards) would autmatically turn an int into an Integer in this context).
The compiler complaint is:
c) Finally, a couple of compiler funnies:
I realise that the above makes no sense (the argument to add should be of the same type as the list members, and it doesn't return anything, so assigning it is meaningless). That said, the compiler complaint is:
Eh? I never used void!
Again, this is wrong (I missed out the '.'). But the usually very helpful compiler simply tells me that there is no qsort method. It would be nice if it said 'there is no method called qsort, how about .qsort? (You see? You give people a lovely compiler that tells idiots what they got wrong, and then the idiots complain it's not idiot-proof enough. That's gratititude for you )
I assume this is the right forum to discuss things like this? I'm assuming that it's definitely the right place for language suggestions (if not bug reports). I also realise I'm just posting what are in effect bug reports - I do promise that one day I'll try to post bug fixes
Here's my effort (please ignore whether or not I've missed something in the implementation i.e. whether or not it will actually sort anything!)
class Test
def qsort(list as List<of dynamic>) as List<of dynamic>
test
#Following doesn't work (can't cast ints to dynamic)
#assert Test().qsort([3,4,1,2])==[1,2,3,4]
pass
body
if list.count==1
return list
else
left as List<of dynamic> = []
right as List<of dynamic> = []
for x in list[1:]
if x<=list[0]
left.add(x)
else
right.add(x)
#return .qsort(left)+.qsort(right) <--doesn't work
#foo = [1,2].add([3,4]) <-- gives an incorrect compiler message
result = .qsort(left)
result.addRange(.qsort(right))
return result
def main is shared
Test().qsort([1,2,3,4])
Which doesn't work, for several reasons (perhaps it would have been more helpful if I'd posted them in a less long-winded manner, but such is life).
The issues are therefore
a) If dictionaries, sets and lists are to be first class syntactic objects (i.e. {:}, {} and []) then I'd really like to be able to perform first class algebra on them.
Simplistically therefore
- Code: Select all
#addition
assert [1,2]+[3,4]==[1,2,3,4] # i.e. extension
assert {1,2}+{3,4}= {1,2,3,4} # i.e. union
assert {1:2}+{3:4}={1:2,3:4} # i.e. union, but here I think we'd also need to specify that the second argument updates and adds to the first (though we would lose addition's expected communtivity, which I realise might be unacceptable) i.e.:
assert {1:2,3:4}+{5:6,3:5}={1:2,3:5,5:6} #That said, python doesn't let you do addition on dictionaries
The other operators are more debatable, but it might be nice to use - for exclusion on Sets (and perhaps \ for intersection?)
We'd also need to specify that + on a list is equivalent to addrange rather than add (i.e. [1]+[2,3]=[1,2,3], not [1,[2,3]]) and is only meaningful for compatible types. Perhaps these are insufficiently obvious to be allowable (after all, that's why people hate operator overloading in the first place) making this a bad idea. At any rate, I do know I want + for Lists
b) I started off trying to make my list a List<of IComparable>, but then it wouldn't accept ints, so I relaxed that to dynamic, but it *still* won't accept ints (I'm assuming because an int is not an Object). (I should say at this point I come from a Java background not a C# background). I realise it's pretty sophisticated, but are there plans to add autoboxing so that this works? (i.e. I think the new versions of Java (1.5 and onwards) would autmatically turn an int into an Integer in this context).
The compiler complaint is:
- Code: Select all
error: Cannot implicitly convert type "System.Collections.Generic.List<int>" to "System.Collections.Generic.List<object>"
c) Finally, a couple of compiler funnies:
- Code: Select all
foo = [1,2].add([3,4]) <-- gives an incorrect compiler message
I realise that the above makes no sense (the argument to add should be of the same type as the list members, and it doesn't return anything, so assigning it is meaningless). That said, the compiler complaint is:
- Code: Select all
error: Keyword "void" cannot be used in this context
Eh? I never used void!
- Code: Select all
foo = qsort([1,2])
Again, this is wrong (I missed out the '.'). But the usually very helpful compiler simply tells me that there is no qsort method. It would be nice if it said 'there is no method called qsort, how about .qsort? (You see? You give people a lovely compiler that tells idiots what they got wrong, and then the idiots complain it's not idiot-proof enough. That's gratititude for you )
I assume this is the right forum to discuss things like this? I'm assuming that it's definitely the right place for language suggestions (if not bug reports). I also realise I'm just posting what are in effect bug reports - I do promise that one day I'll try to post bug fixes