Utilize CanBuildFrom implicits instead of relying on concrete builders#57
Conversation
|
FYI - I just also checked in a change to improve the performance of TraversableSerializer's |
There was a problem hiding this comment.
Can't we do CanBuildFrom[Nothing, T, C] and avoid taking c?
There was a problem hiding this comment.
Well, the CanBuildFrom implicit needs to capture the actual type of the originating collection in its first type parameter, so using Nothing would not do what we want here. Using CanBuildFrom[C, T, C] lets us get a builder that knows how to take elements from C (of type T), and rebuild another collection of the same type C. If we use Nothing then it loses that concrete collection type for the From type parameter.
There was a problem hiding this comment.
But we never use the From parameter. It seems strange that we actually throw c away. I think this code would work just as well with:
def forTraversableSubclass[T, C](isImmutable: Boolean = true)(implicit mf: ClassManifest[C], cbf: CanBuildFrom[Nothing, T, C]): Kryo = {Would that cause any issue since we only use the def apply(): Builder[T, C] method?
There was a problem hiding this comment.
If we get rid of the c param, won't we have to register the traversables
like this?
.forTraversableSubclass[Any, Queue[Any]]()
.forTraversableSubclass[Any, List[Any]]()I was keeping the c param to help the type inferencing out and to
properly bind C and T. I futzed with it for a good bit and ended up
with what's in the pull request. My scala-fu may be missing something,
though. Maybe you could try out the code from my fork some time and try to
get it to work with CanBuildFrom[Nothing, T, C]?
There was a problem hiding this comment.
Fair enough. Let's merge what we have and if we can simplify later we will.
|
Thanks for taking this on! |
|
Thanks! I had a lot of fun doing it. |
Utilize CanBuildFrom implicits instead of relying on concrete builders
This pull request addresses one of the TODOs in the code base around utilizing
CanBuildFromto convert to the appropriate collection types. This basically makes the previously required introspection/cloning around mutable collections obsolete and has been removed in this pull request in favor ofCanBuildFrom.One thing to note in this pull request is that we're employing the known 2.9.x workaround
c: C with Traversable[T]to get around a compiler inference bug. This issue has been fixed in 2.10 (the compiler lets you just sayc: Cas you would expect to work). I tested this workaround for things that get converted to Traversable, e.g. strings and arrays as well as BitSet. I couldn't find a collection where the workaround would fail, although there might be a case that I'm unaware of (and not already covered in the unit tests). All the unit tests pass, including the mutable ones!