Extension function .each on Collection<T>


#1

My first extension function and my first function literal:

https://gist.github.com/2019572

``

package litfunc

import java.util.ArrayList
import java.util.Collection

fun main(args : Array<String>) : Unit {
  val l = ArrayList<String>()
  l.add(“one”)
  l.add(“two”)
  l.add(“buckle my shoe”)
  l.each { it ->
  println(it)
  }
  /*
  Output is:
  one
  two
  buckle my shoe
  */
}

fun <T> Collection<T>.each(fn : (T) -> Unit) {
  for( item in this) {
  fn(item)
  }
}


Suggestions on how I can improve this welcome.

Mark


#2

Looks good - I guess supporting variance should be added too (taking "in T").

BTW here’s the current extension functions available on Collection<T> - we’ve got a method we called foreach (rather than each) as it seemed a little more descriptive.


#3

Please rename it "forEach", let's stop carrying this broken legacy around :-)


Cédric


#4

Good point :)


#5

jstrachan wrote:

Looks good - I guess supporting variance should be added too (taking “in T”).

BTW here’s the current extension functions available on Collection<T> - we’ve got a method we called foreach (rather than each) as it seemed a little more descriptive.


Nice.  I did not know these extensions existed.


#6

Done (you might need to hit refresh in your browser) to see the updated extension methods on Collection<T>


#7

Awesome, thanks James!


#8

I can try to work on the "in T" annotation, but I'm so new at this stuff that someone will have to check my work.  It may not be worth me even attempting for that reason.

But… if using the “in T” form is the right thing to do, then maybe get this into the build before people potentially start using unsupported use-site forms.  Or something like that.


#9

Agreed. BTW we love contributions if you fancy a challenge! There's a pending issue to double check all the extension functions are using the right in/out types. http://youtrack.jetbrains.com/issue/KT-1409

To do this we really need more test cases that use the extension functions with collections of different kinds of objects (e.g. collections of People and Managers) to see where we’ve missed some in/out type parameters.
e.g. like these tests:  https://github.com/JetBrains/kotlin/blob/master/libraries/testlib/test/CollectionTest.kt

Doing this will help us all learn about Kotlin, generics & variance too - and help firm up some missing areas in the standard library.

If you fancy helping, please fork kotlin on github, try add new tests to spot missing in/out type parameters & when you hit an issue, try figuring out a fix and then sending us a pull request? Feel free to ask here if things are not clear!


#10

This should probably go in a new thread, but I'll start here:

Is there any documentation for contributors on how to setup your development environment?  I performed the two Ant steps indicated in README.md, then opened the Kotlin top level project (as-directory, hoping it would do the right thing).  The kotlin runtime was found in lib/kotlin-runtime.jar, and the IDE plugin added it.

Attached is a screenshot of an open file, with red all over it.



kotide.png (165 KB)

#11

I had the exact same problem with one more symptom: the top of the editor was showing the yellow flash "The Kotlin runtime is not installed, click here to fix this" but clicking the link produced absolutely no result.

I ended up creating a brand new project and things seem to work there.


#12

Still no luck.

I’m using EAP 11.1 116.32.

I clean the existing project files thusly, from the top level directory:

``

flist=$(find . -name *.iws -o -name *.ipr -o -name *.iml -o -name .idea)
for i in $flist; do
  rm -rf $i
done

Then

ant -f update_dependencies.xml
ant -f build.xml

Then I create a new project from existing sources, and point at the top level directory.  The IDE finds a bunch of source trees and libs, then opens the workspace.  I open a .kt file, such as /TemplateCoreTest.kt, but after a few seconds of indexing, I still get red all over the editor.



tct.png (197 KB)

#13

The IDEA project is already in the repository, so, you can use it instead of creation of the new one. Additionally, Kotlin plugin should be deleted before working with Kotlin project in IDEA.


#14

So its a little fiddly working on the kotlin project; as its kinda 2 projects; the compiler & IDEA plugin - then a bunch of libraries written in kotlin.

I’ve just updated the ReadMe.md to try be a bit more clear; typically you either want to:

  • work on the compiler/IDE plugin - in which case you work on the root project in an IDEA with no Kotlin plugin installed
  • work on the kotlin libraries - in which case you either use the above to run a  new IDEA with the Kotlin plugin - or install the Kotlin plugin and open the libraries project

I hope that helps! Am sure we could improve these instructions further though :)