uqbar-project / wollok

Wollok Programming Language
GNU General Public License v3.0
60 stars 16 forks source link

Strange behavior modifying collection himself in forEach #1013

Closed lspigariol closed 7 years ago

lspigariol commented 8 years ago

in REPL

var lista = [1,2,3,4] lista.forEach{e=>lista.remove(e)} wollok.lang.Exception at wollok.lang.List.fold(initialValue,closure) [classpath:/wollok/lang.wlk] at wollok.lang.Collection.forEach(closure) [classpath:/wollok/lang.wlk] lista [2, 3, 4]

similarly

var lista = [1,2,3,4] lista.forEach{e=>lista.add(e)} wollok.lang.Exception at wollok.lang.List.fold(initialValue,closure) [classpath:/wollok/lang.wlk] at wollok.lang.Collection.forEach(closure) [classpath:/wollok/lang.wlk]

lista [1, 2, 3, 4, 1]

fdodino commented 8 years ago

This is a normal problem in all languages. You should get a copy of the collection. I wonder if we can check there is no remove in a forEach loop.

npasserini commented 8 years ago

I agree it is a common problem, but I think it could be solved (not sure, need to research a little).

Yet the question remains... should be resolve it? Because if we do we would allow for algorithms that afterwards will not work in any other language. What do you think?

To forbid the remove/add inside foreach... does not seem easy.

tesonep commented 7 years ago

The only idea I get to this is throwing a more descriptive exception, something that looks a like a ConcurrentModificationException of Java, but with a better name and explanation, like YouTryToModifyTheCollectionWhileYouWereIteratingItException.

Other idea would be that forEach and the other iterations always works on a copy of the collection, but that would make the collection framework very different than other languages. I think this can be an issue if the students then "migrate" to other languages where this does not happens.

However, if we want to go this path we should have a clear idea of what are the advantages to the students.

npasserini commented 7 years ago

I like the idea of copying the collection or whatever solution that allows us to modify the collection while it is being iterated.

I know that other languages do not work that way, but I do not think it is a nice thing to explain to a beginner, they will have time to learn workarounds and implementation problems later.

Maybe it would be nice to be able to have an implementation that avoids copying until it is absolutely necesary. Even, copying is not trivial, because if you copy and iterate, you should avoid that the copied list does not try to copy itself again... so a lower level mechanism is required anyway.

2017-01-17 11:51 GMT+01:00 Pablo Tesone notifications@github.com:

The only idea I get to this is throwing a more descriptive exception, something that looks a like a ConcurrentModificationException of Java, but with a better name and explanation, like YouTryToModifyTheCollectionWhi leYouWereIteratingItException.

Other idea would be that forEach and the other iterations always works on a copy of the collection, but that would make the collection framework very different than other languages. I think this can be an issue if the students then "migrate" to other languages where this does not happens.

However, if we want to go this path we should have a clear idea of what are the advantages to the students.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/uqbar-project/wollok/issues/1013#issuecomment-273105040, or mute the thread https://github.com/notifications/unsubscribe-auth/AEa1OUOARNoiSvDxEkvPGJyVyDfUwdMiks5rTJ1FgaJpZM4KRHW0 .

tesonep commented 7 years ago

I think that copying always is not a problem. Wollok is not intended to have a collection with lots of objects.

On Wed, Jan 18, 2017 at 12:31 PM, Nico Passerini notifications@github.com wrote:

I like the idea of copying the collection or whatever solution that allows us to modify the collection while it is being iterated.

I know that other languages do not work that way, but I do not think it is a nice thing to explain to a beginner, they will have time to learn workarounds and implementation problems later.

Maybe it would be nice to be able to have an implementation that avoids copying until it is absolutely necesary. Even, copying is not trivial, because if you copy and iterate, you should avoid that the copied list does not try to copy itself again... so a lower level mechanism is required anyway.

2017-01-17 11:51 GMT+01:00 Pablo Tesone notifications@github.com:

The only idea I get to this is throwing a more descriptive exception, something that looks a like a ConcurrentModificationException of Java, but with a better name and explanation, like YouTryToModifyTheCollectionWhi leYouWereIteratingItException.

Other idea would be that forEach and the other iterations always works on a copy of the collection, but that would make the collection framework very different than other languages. I think this can be an issue if the students then "migrate" to other languages where this does not happens.

However, if we want to go this path we should have a clear idea of what are the advantages to the students.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/uqbar-project/wollok/issues/1013# issuecomment-273105040, or mute the thread https://github.com/notifications/unsubscribe-auth/ AEa1OUOARNoiSvDxEkvPGJyVyDfUwdMiks5rTJ1FgaJpZM4KRHW0 .

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/uqbar-project/wollok/issues/1013#issuecomment-273452411, or mute the thread https://github.com/notifications/unsubscribe-auth/AAVsf6IkcgeWLyFK0ZHNP0vxnfl7vHAXks5rTfgJgaJpZM4KRHW0 .

-- Pablo Tesone. tesonep@gmail.com