Some suggestions of standard library functions


#1

Hello everyone,

I’ve recently started to use Kotlin and I have some things that force me to write extension functions.

  1. There’s no standard library function to transform a collection of pairs into map. I can write val m = mapOf(*listOfPairs.copyToArray()), but it looks like a hack. It would be nice to have fun Iterable<Pair<T,V>>.toMap(): Map<T, V> in standard library.

  2. I cannot write someMap.map { key, value -> … }, which would be nice also.

  3. Analog of Scala’s “sf” string pattern (with printf formatting) would be VERY appreciated.

  4. Using IDEA with Java I got used to write right part of expression, than hit Alt-Enter “Introduce local variable”, I was suprised this is not yet implemented in Kotlin plugin. It’s no big deal, but it breaks my coding pattern :slight_smile:

  5. In java I can take some code in brakets to make all variable names local, why I cannot do this in Kotlin?

Thank you!


#2

1. There's no standard library function to transform a collection of pairs into map. I can write val m = mapOf(*listOfPairs.copyToArray()), but it looks like a hack. It would be nice to have fun Iterable<Pair<T,V>>.toMap(): Map<T, V> in standard library.

  1. I cannot write someMap.map { key, value -> … }, which would be nice also.

Contributions to the standard library are very welcome. Also feel free to file an issue in http://youtrack.jetbrains.net/issues/KT

3. Analog of Scala's "sf" string pattern (with printf formatting) would be VERY appreciated.

Yes, it would be nice to have it, feel free to file an iisue.

4. Using IDEA with Java I got used to write right part of expression, than hit Alt-Enter "Introduce local variable", I was suprised this is not yet implemented in Kotlin plugin. It's no big deal, but it breaks my coding pattern :)

We should support Alt+Enter as well, but for now we only do it throgh explicit "Introduce variable" refactoring (Alt+Ctrl+V). Again, feel free to file an issue

5. In java I can take some code in brakets to make all variable names local, why I cannot do this in Kotlin?

Use the run() function:

run {   val foo = "this is local"   println(foo) }