fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> { }

Of course both of the following work:

fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {   // Use spread operator   return listOf(*listOfLists.toTypedArray(), toAppend); }

fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
  // Turn to mutable list
  val ret = listOfLists.toArrayList()
  ret.add(toAppend)
  return ret
}


But this is confusing to users who might expect + to work. Due to type erasure, + will always have quirks like this. I had to add the following extension:

operator fun <T, C : Collection<T>> List<C>.plus(toAppend: C): List<C> {
    val ret = java.util.ArrayList<C>(this.size + 1)
    ret.addAll(this)
    ret.add(toAppend)
    return ret
}


You have two choices:

The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map:

val map = friends.associateBy({it.facebookId}, {it.points})

The second, less performant, is to use the standard map function to create a list of Pair which can be used by toMap to generate the final map:

val map = friends.map { it.facebookId to it.points }.toMap()



캬 멋집니다~



https://medium.com/@joongwon/kotlin-kotlin-%ED%82%A4%EC%9B%8C%EB%93%9C-%EB%B0%8F-%EC%97%B0%EC%82%B0%EC%9E%90-%ED%95%B4%EB%B6%80-part-3-59ff3ed736be?source=rss-1ee57944eab8------2

'링크모음 > 코틀린' 카테고리의 다른 글

Sealed class + when 자세한 설명  (0) 2017.11.21
접근 제어자 관련 링크  (0) 2017.11.20
kotlin-let-apply-run-with-use 사용방법  (0) 2017.11.17
코틀린 1.1 한글번역 문서  (0) 2017.11.02
코틀린 Junit 사용하기  (0) 2017.09.02

코틀린 Junit test

https://proandroiddev.com/using-kotlin-for-tests-in-android-6d4a0c818776



핵심은 


    sourceSets {
test.java.srcDirs += 'src/test/kotlin'
}
}

afterEvaluate {
android.sourceSets.all { sourceSet ->
if (!sourceSet.name.startsWith("test"))
{
sourceSet.kotlin.setSrcDirs([])
}
}
}


으로 해서 릴리즈땐 제거 해주고


testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_ver"


을 추가 해주는게 핵심..

kapt {
generateStubs = true
}


를 왜 해줘야 하는지..ㅠㅠ 몇시간을 날린거야..!!

http://pluu.github.io/blog/kotlin/2017/07/23/kotlin-generatestubs/

+ Recent posts