|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
[scala] polymorphic collection construction in 2.8I am trying to write a generic function to collect elements from a Responder
into an actual collection, but to be generic about the type of collection. I get to something like: def collect[A, C <: Traversable[A]] (r: Responder[A]) : C = { val b: Builder[A,C] = ... // new Builder[A, C] ?? for (val a <- r) b += a return b.result but what should ... be? it can't be new Builder[A, C], since Builder is just a trait and you need a colection specific subclass Every companion object of a collection class has newBuilder method, but how do you get at it in a generic way? Colection classes do have a companion method, but that requires a collection instance to begin with. I have thought about something like Traversable.empty[A].genericBuilder[A, CC] but that only covers collection classes that correspondto unary functors, ie no Maps. I also looked at the implicit newBuilderFactory mehods but I did not get anywhere with those either. thanks Daniel |
|
|
[scala] Re: polymorphic collection construction in 2.8On Sat, Oct 17, 2009 at 11:01 PM, Daniel Mahler <dmahler@...> wrote:
> I am trying to write a generic function to collect elements from a Responder > into an actual collection, but to be generic about the type of collection. > I get to something like: > > def collect[A, C <: Traversable[A]] (r: Responder[A]) : C = { > val b: Builder[A,C] = ... // new Builder[A, C] ?? > for (val a <- r) b += a > return b.result > > but what should ... be? > it can't be new Builder[A, C], since Builder is just a trait > and you need a colection specific subclass > Every companion object of a collection class has newBuilder method, > but how do you get at it in a generic way? > Colection classes do have a companion method, > but that requires a collection instance to begin with. > > I have thought about something like Traversable.empty[A].genericBuilder[A, CC] And that is not even how genericBuilder works :) > but that only covers collection classes that correspondto unary > functors, ie no Maps. > I also looked at the implicit newBuilderFactory mehods > but I did not get anywhere with those either. > > thanks > Daniel > |
|
|
Re: [scala] Re: polymorphic collection construction in 2.8The best I can come up with is:
def collect[A, CC[X] <: Traversable[X]](r: Responder[A])(to: GenericCompanion[CC]) = { val b = to.newBuilder[A] for (a <- r) b += a
b.result } Unfortunately you have to specify explicitly what kind of collection you want: collect(responder)(List) or
collect(responder)(Set) --j On Sat, Oct 17, 2009 at 2:33 PM, Daniel Mahler <dmahler@...> wrote:
|
| Free embeddable forum powered by Nabble | Forum Help |