Forums

Pair sample class

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

Pair sample class

Postby Charles » Mon Jul 20, 2009 3:12 am

I whipped up a small sample class for storing a pair of values using `dynamic` (as opposed to alternatives such as `Object` or a generic).

As webnov8 pointed out, this is somewhat reminiscent of tuples, but I didn't take it that far.

See attached. Comments and questions are welcome.
Attachments
Pair.cobra
(3.03 KiB) Downloaded 1009 times
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class based on dynamic

Postby jonathandavid » Tue Jul 21, 2009 5:12 am

Very neat. To make method tests less intrusive, you could consider adding an "assert" block where every statement gets asserted. Compare the current:

def compareTo(p as Pair) as int
test
assert Pair('a', 'b') < Pair('b', 'c')
assert Pair(2, 3) > Pair(2, 2)
assert Pair(nil, 'a') < Pair(nil, 'b')
assert Pair(nil, nil) < Pair(1, 1)
body
diff = .compare(.a, p.a)
if diff == 0, diff = .compare(.b, p.b)
return diff


With:

def compareTo(p as Pair) as int
test
assert Pair('a', 'b') < Pair('b', 'c')
Pair(2, 3) > Pair(2, 2)
Pair(nil, 'a') < Pair(nil, 'b')
Pair(nil, nil) < Pair(1, 1)
body
diff = .compare(.a, p.a)
if diff == 0, diff = .compare(.b, p.b)
return diff
jonathandavid
 
Posts: 159

Re: Pair sample class based on dynamic

Postby Charles » Sun Jun 10, 2012 1:38 am

I have expanded on this with Pair<of T> and Pair<of TA, TB>. Like C#, Cobra allows class names to be overloaded based on the number of generic arguments. Comes in handy.

This can now be found under the Supplements directory in a Cobra workspace, and also browsed online:
http://cobra-language.com/trac/cobra/br ... Pair.cobra

Comments and questions are welcome.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class

Postby Charles » Sun Jul 01, 2012 4:27 pm

I have added Pair<of T> and Pair<of TA, TB> to the Cobra standard library so that they are immediately available (just like List<of T>, Set<of T>, Dictionary<of TK, TV>, etc.). changeset:2730

I haven't decided to include the dynamic version at this point. It's not strictly necessary and Cobra may infer generic args on object creation at some point in the future, making it even less interesting. You can still copy it out of the Supplements directory and include it in your project if you want it.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class

Postby jaegs » Mon Jul 23, 2012 4:05 pm

What is "Repeated Pair<of>"?
class X
def main
pairs = [Pair<of int>(1,2), Pair<of int>(3,4), Pair<of int>(1,2), Pair<of int>(3,4)]
trace pairs

# trace : pairs=List<of Pair<of int>>[Pair<of int>[1, 2], Pair<of int>[3, 4], (Repeated Pair<of int>), (Repeated Pair<of int>)]
# - at Test.cobra:6
# - in X.main

#Cobra svn:2738 (post 0.8) / 2012-07-08 on Mono 2.10.9 CLR v4.0.30319 on Mac OS X 10.6.8
jaegs
 
Posts: 58

Re: Pair sample class

Postby Charles » Tue Jul 24, 2012 12:21 am

This behavior comes from Cobra.Core.StringMaker which avoids repeating nested lists, dictionaries, etc:
class X
def main
t = [1, 2, 3, 4, 5, 6, 7, 8, 9]
trace [t, t]
/#
trace : [t, t]=List<of List<of int>>[List<of int>[1, 2, 3, 4, 5, 6, 7, 8, 9], (Repeated List<of int>)]
- at x-pairs.cobra:6
- in X.main
#/

It accomplishes this primarily by checking for IEnumerable, though it already makes an exception for String. I have added an exception for Pair. So this is fixed now.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class

Postby jaegs » Thu Aug 02, 2012 2:35 pm

Do you think it's possible to make Pair<of T> a subclass of Pair<of T, T> ?

A method that takes a Pair<of TA, TB> won't accept Pair<of T> even though conceptually it should. Hence a second method that takes Pair<of T> is required. Pair<of T> quickly becomes more of an annoyance than a convenience.
jaegs
 
Posts: 58

Re: Pair sample class

Postby Charles » Thu Aug 02, 2012 2:43 pm

I'm about to step away so I can't try it now, but it certainly seems feasible:
class Pair<of T> inherits Pair<of T, T>
...

I understand your complaint. Thanks for bringing this up.

I was contemplating making them structs but .NET disallows inheritance with structs. I suppose this would preclude that. I think the issue you bring up is the larger one, however.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class

Postby jaegs » Sat Aug 04, 2012 9:12 am

In the future, I plan on investigating the inference of type args for object instantiation. For example, Pair(5, 's') instead of Pair<of int, String>(5, 's'). Wouldn't that be nice?


Sounds good. However unless there is some sort of literal syntax, I imagine that for personal use I would do something like
class P<of TA, TB> inherits Pair<of TA, TB>
pass


in order to do
lst = [P(c'a', 1), P(c'd', 5), P(c'e', 3)]
jaegs
 
Posts: 58

Re: Pair sample class

Postby Charles » Sat Aug 04, 2012 5:08 pm

If we follow through on "alias", you could use that.

Also, this is intended for any generic class. It has been pointed out that C# will infer generic type args for methods but not for constructors which feels inconsistent. And not only is it less convenient, but it is a deterrent to making a non-generic class generic since it will break all object creations. Cobra has these same limitations.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 8 guests