SAM question


#1

short question: why does this not compile?

import java.util.stream.Stream
import java.util.Collections

fun main(args: Array<String>) {
  val arrayList = arrayListOf(1, 2, 3)
  Collections.sort(arrayList, {(a:Int,b: Int) -> a- b })
  Stream.iterate(“hello”, {(t: String?) -> “hello” } )
}

SwingUtilities.invokelater { stuff() } shows no error

in java8, this compiles:
Collections.sort(new ArrayList<String>(), (o1,o2) -> o1.compareToIgnoreCase(o2));


#2

Unfortunately, we don't support JDK 8 yet. We are planning do it after JDK 8 is released.

In this particular case, our SAM check thinks that Comparator (used in sort()) and UnaryOperator (used in iterate()) have more than one abstract method, so they are not SAM.

Good news that Kotlin standard library covers your cases and the code is much cleaner, look:

fun main(args: Array<String>) {   val arrayList = arrayListOf(1, 2, 3)   arrayList.sortBy { it }   // or:   arrayList.sort()

  “hello”.forEach { t -> println(t) }
  // or this:
  for (t in “hello”) {
  println(t)
  }
}


#3

hm, why would it think that a comparator has more than one method? because of default methods?


#4

Yes, because of default methods.