Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2845-Error-in-CollectionflatCollect-b-as-String #4594

Conversation

kasperosterbye
Copy link
Contributor

Fixes #2845

The reason for the error is that the method #flatCollect:as: inserts elements into the collection type given as second argument. String is immutable, so that is not possible.

The reason for the error  is that the method `#flatCollect:as:` inserts elements into the collection type given as second argument. String is immutable, so that is not possible.
@kasperosterbye
Copy link
Contributor Author

Fixes #2845 Error in Collection>>flatCollect: b as: String

Analysis:
The same error is also possible to provoke using:
'#(#(1 2) #(4 3)) flatCollect: #yourself as: Array'

The reason is that the method #flatCollect:as: inserts elements into the collection type given as second argument. String and Array are both immutable, so that is not possible.

The solution is to flatten the elements into a temporary OrderedCollection, and then return an instance of second argument build from that temporary OrderedCollection.

I am a bit uncertain if there are any unforseen effects of using OrderedCollection, so I submit this PR to see how the tests go.

Collection >> flatCollect: aBlock as: aCollectionClass
"Evaluate aBlock for each of the receiver's elements and answer the
list of all resulting values flatten one level. Assumes that aBlock returns some kind
of collection for each element. Equivalent to the lisp's mapcan"

| col |
col := OrderedCollection new: self size.
self do: [ :each | col addAll: (aBlock value: each) ].
^ aCollectionClass withAll: col 

@MarcusDenker
Copy link
Member

from the perspective of the tests: it seems to not introduce any problems

@MarcusDenker MarcusDenker merged commit 4e29b17 into pharo-project:Pharo8.0 Sep 19, 2019
@kasperosterbye kasperosterbye deleted the 2845-Error-in-CollectionflatCollect-b-as-String branch January 22, 2020 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in Collection>>flatCollect: b as: String
2 participants