2 Replies Last post: Feb 27, 2012 12:23 AM by Arul Dhesiaseelan  
Arul Dhesiaseelan Newbie 2 posts since
Feb 26, 2012
Currently Being Moderated

Feb 26, 2012 3:07 AM

Kotlin collection.map supported yet

I am trying to compile this sample which maps users by lastname, but it fails with "Compiler terminated with exit code: -1". Am I missing anything?

 

import std.util.*
import std.*
 
fun main(args : Array<String>) {
    val users = arrayList(
    User("John", "Doe1", 31),
    User("John", "Doe2", 32),
    User("John", "Doe3", 33))
 
    val usersByLastName = users.map {it.lastName}
    for (item in usersByLastName) {
        println(item)
    }
}

 

Here is the User.kt:

public class User(val firstName: String, val lastName: String, val age: Int) {
    fun toString() ="$firstName $lastName, age $age"
}
Andrey Breslav Apprentice 832 posts since
Jun 11, 2007
Currently Being Moderated
Feb 26, 2012 10:06 AM in response to: Arul Dhesiaseelan
Re: Kotlin collection.map supported yet

No, you are not. It's a bug in type inference. Will be fixed soon.

Workaround:

import std.util.*
import std.*
 
fun main(args : Array<String>) {
    val users = arrayList(
    User("John", "Doe1", 31),
    User("John", "Doe2", 32),
    User("John", "Doe3", 33))
 
    val usersByLastName = users.map {(it : User) -> it.lastName}
    for (item in usersByLastName) {
        println(item)
    }
}

More Like This

  • Retrieving data ...