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".

Re: Pair sample class

Postby Charles » Sat Aug 04, 2012 9:09 pm

Re: Pair inheriting Pair:

Right now Pair<of T> has a strongly typed indexer and enumerable interface:
class Pair<of T> implements IEnumerable<of T>

get [i as int] as T

class Pair<of TA, TB> implements System.Collections.IEnumerable

get [i as int] as dynamic?

I think the strongly typed enumerable could be saved, but not the indexer. Maybe that's okay, but I wanted to point it out. Too bad .NET does not have a concept of "greatest common denominator" for types:
class Pair<of TA, TB> implements System.Collections.IEnumerable 

get [i as int] as gcd(TA, TB)

Cobra has such a concept in certain places like when it infers the type of collection literals such as [Square(), Circle()] which would be List<of Shape>. However, using it in return types would be fairly meaningless because run-time construction of .NET generics wouldn't implement it.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class

Postby jaegs » Sun Aug 05, 2012 9:43 am

Seems like a worthwhile sacrifice.
jaegs
 
Posts: 58

Re: Pair sample class

Postby Charles » Fri Aug 31, 2012 9:47 pm

I've made some progress on this, but when I try it out for the intended purpose, the compiler falsely complains that Pair<of int> cannot be passed where Pair<of int, int> is expected. I'll have to work that out.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Pair sample class

Postby Charles » Fri Aug 31, 2012 11:31 pm

False alarm on the type compatibility.

This is done now in changeset:2790.

Also, I noticed Pair<of TA, TB>'s .init method was still taking dynamically typed params, no doubt a left over from the original Pair class which was all dynamic. This has been improved as appropriate for generics:
-       cue init(a, b)
+ cue init(a as TA, b as TB)


One notable change is that the indexer on Pair<of TA, TB> must necessarily return "dynamic?" since nothing is known about what TA and TB have in common. Hence there were several changes in MultiList such as:
-               _shape = for range in ranges get range[1] - range[0]
+ _shape = for range in ranges get range.b - range.a

- addr += (index + _ranges[i][0]) * _strides[i]
+ addr += (index + _ranges[i].a) * _strides[i]
...because .a and .b can be strongly typed since they are separate properties.

Please take this for a test spin and let me know if you have any issues.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 28 guests