Seems easy enough. I'll try to make a pull request tomorrow.
Probably won't call it `to` as I'm a bit worried about conflicts. But I figure I'll make the pull request and see what happens.
Note:
def to[Col[_]](implicit cbf: CanBuildFrom[Repr, A, Col[A]]) = {
val b = cbf(repr)
b ++= this.seq
b.result
}
Or some such. Builders are already there for everything.
Not sure to is the best name though as I'm worried about breaking other code. Anyone know of conflicts?
Right... unchecked variance might be in order.... hmm ...
Right... unchecked variance might be in order.... hmm ...
A lot of collection methods are optimised with class pattern matches. IIRC ++= and sizeHint are both done in this way.
Given how often both are used in methods, they're more likely to have hotspots optimisations, but of course we need to profile to be sure.
Also, even in Traversable you need to use Nothing as base type in CanBuildFrom, since effectively you're doing a breakOut.
That sounds reasonable, except why "Into"? We don't intoString, intoVector, etc., but it's exactly the same process.
Is copyTo not okay for some reason?
--Rex
Matthew
mailto: turingatemyhamster@gmail.comgchat: turingatemyhamster@gmail.com
--Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle University
mailto: turingatemyhamster@gmail.comgchat: turingatemyhamster@gmail.com
irc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
--Rex
Matthew
mailto: turingatemyhamster@gmail.comgchat: turingatemyhamster@gmail.com
--Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle University
mailto: turingatemyhamster@gmail.comgchat: turingatemyhamster@gmail.com
How about copyTo, but with an argument that defaults to the appropriate empty collection type - kill two birds with one stone.
> I moved to copyTo. Got some complaints, we're now on "build" as the name for the method.
Any objections?
> I moved to copyTo. Got some complaints, we're now on "build" as the name for the method.
The commit message says "copyInto" and the actual patch says "convertTo".
Any objections?
Yes, frankly I don't think it carries its own weight.
Adding methods has a cost, both for those maintaining it in the future and those who actually have to use it.
This is yet-another method on a collection API with close to 100 methods, with a non-intuitive, non-established name.
I'm quite a bit concerned about the style this got added. Especially Paul (and a lot of others) did a lot of work to keep the API as clean as possible despite its heavy-weight. We should not drop that.
"to" would be great, because it has the same cost on the maintenance side as every other method name, but almost zero cost on the user side, because the pattern is already established.
A user will type "to" and will wait for auto-completion to kick in. Auto-completion will offer toList, toMap, toArray, toStream ... and probably to[DecideYourself]. It is completely obvious what "to" is supposed to do.
Apart from that "to" is already known from and established in third-party libraries for exactly this use-case.
Imho either call it "to" or remove it. Adding yet-another thing to learn is not worth the additional weight.
"to" would be great, because it has the same cost on the maintenance side as every other method name, but almost zero cost on the user side, because the pattern is already established.
I don't disagree with you about the convention and "to". However, I think the names toVector, toBuilder, etc. are somewhat poor. Specifically, they don't denote the copying that happens in the general case.
A user will type "to" and will wait for auto-completion to kick in. Auto-completion will offer toList, toMap, toArray, toStream ... and probably to[DecideYourself]. It is completely obvious what "to" is supposed to do.
Apart from that "to" is already known from and established in third-party libraries for exactly this use-case.
Imho either call it "to" or remove it. Adding yet-another thing to learn is not worth the additional weight.IMHO removing all the other "to" methods would be wise. We don't need them with one good convertTo.I'd be fine naming the method "to" and deprecating all the other "toXYZ" methods myself. They're extra baggage on the API since we know one method could do the work.I still prefer copyTo/convertTo over just plain old "to" personally, because the name denotes a bit more meaning.However, the point is that this new method makes the old ones unnecessary. I think that's a win. What's missing is deprecations. What do people think about deprecating the "toXYZ" methods in favor of the generic convertTo?(Note: Deprecating all those methods will take a bit of work, but something I'd like to do. They can all be implemented as convertTo, but that will require some refactoring of other collection methods to ensure we don't have stack overflows and such.)
I really don't see a single reason why we need to break working code and need to have an inconsistent API. This really feels like some second-system fallacy.
Btw: Afaik toVector wasn't added because there were concerns about creating huge dependencies (for Android). It would make sense to check whether the addition drags an tho whole parallel framework. Currently a proguarded HellowWorld is 170 kb, we should ckeck if this is still the case when adding toVector.
Btw: Afaik toVector wasn't added because there were concerns about creating huge dependencies (for Android). It would make sense to check whether the addition drags an tho whole parallel framework. Currently a proguarded HellowWorld is 170 kb, we should ckeck if this is still the case when adding toVector.
+1 for "to".
So.... I am working on an adaption to remove all the "to" methods from collections and put them in type classes. Perhaps that would solve your concerns?
If done correctly, the toXYZ methods become extension methods. That should hopefully reduce bytecode significantly, but I need to test it out.As far as calling the method "to" I can see what you mean about convention. It's not too late to change the name. However, the name "to" still feels short to me. Perhaps we just do it and see how much breaks in the next milestone/nightly?
- Josh
I can only reiterate my previous statement that I think none of those methods should copy immutable collections unnecessarily.
On Tue, Jun 19, 2012 at 11:06 AM, Stefan Zeiger <sze...@novocode.com> wrote:
I can only reiterate my previous statement that I think none of those methods should copy immutable collections unnecessarily.
If you really want to deal with that, you should try to deal with the below, because one must define "unnecessarily".
As I have mentioned before, I do not think mutable and immutable collections should share implementation. What possible code can go here which is anything but a latent bug to some subset of subclasses.
I'd define it as "source is immutable and target is a subtype of source". Not copying mutable collections is just asking for trouble. (AFAICT there is no toXXX method right now which creates a mutable collection, so the issue does not arise without a generic "to").
On Jun 19, 2012 9:00 PM, "Josh Suereth" <joshua....@gmail.com> wrote:
> Ok, so I'm going to see if I can enlist any help here. What I'm trying to do is *move* all the "to" methods into extension methods.
I'm interested what effect this change has on compilation time (if any).
Best,
Ismael
Yeah. Won't know until there is a working patch
Just to clarify: I think this is the perfect use case to test using real extension methods for both performance (compile/run time) and versatility.
If it all works out, it could drastically help us in maintaining an ABI and still fixing issues...
Ok, so thinking back, I think there are rather compelling reasons to name the method "to". So can we all agree to try "to"? I can change what's in trunk to use to then, and we'll see if it breaks any third-party code during the next milestone/RC whatever happens first.Also, note: This generic "to" method always copies unless we do some kind of work with CanBuildFrom, like Stefan suggests. (Which is a good idea).