Kotlin / kotlin-koans

Kotlin workshop
MIT License
2.6k stars 1.41k forks source link

DateRange no longer works in forEach loop? #30

Closed nicholasq closed 9 years ago

nicholasq commented 9 years ago

Was there a language change regarding using Ranges in a forEach loop? (I'm on kotlin 1.0 beta)

In iii_conventions/_RangeTo.kt :

for (date in firstDate..secondDate) {
       handler(date)
}

Will not compile for me. I am pretty sure I have my DateRange implemented correctly.

Thank you! Loving Kotlin!

yole commented 9 years ago

Which error exactly are you getting?

nicholasq commented 9 years ago

The error in IDEA says: using rangeTo(T): ComparableRange is an error. This range implementation has unclear semantics and will be removed soon.

I have my DateRange implemented just like the resolution in this repo.

Bluehorn commented 9 years ago

using rangeTo(T): ComparableRange is an error. This range implementation has unclear semantics and will be removed soon.

I had the same error and was wondering for a while about what was going on.

I found the reason by using "Go To Declaration" (F3 with the Eclipse key bindings) on the .. operator, which took me to rangeTo for Comparable, not to my implementation as I would have expected. Looking at that deprecated definition I noticed that it is prefixed with public operator fun.

Going back to my implementation I found that I did not declare my fun rangeTo as operator, so Kotlin did not use it. Of course it would be helpful to have a warning here if a function with the required name is available but not declared as operator. :-)

Anyway, that fixed it for me. All tests are passing now and I really enjoyed the workshop. Thanks!

Edit: Fixed Markdown formatting. I am using Jira too often... :blush:

nicholasq commented 9 years ago

Ah! Thank you Bluehorn! This fixed it.