kotlin-extension

Unresolved reference: async in Kotlin in 1.3

ε祈祈猫儿з 提交于 2020-06-25 10:50:14
问题 I have multi module kotlin gradle project in github here. One of my sub project introducing-coroutines with build file build.gradle.kts file is here The contents of build.gradle.kts is - import org.jetbrains.kotlin.gradle.dsl.Coroutines import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { java kotlin("jvm") version "1.3.11" } group = "chapter2" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { compile(kotlin("stdlib-jdk8")) compile("org.jetbrains.kotlinx

Unresolved reference: async in Kotlin in 1.3

心已入冬 提交于 2020-06-25 10:49:20
问题 I have multi module kotlin gradle project in github here. One of my sub project introducing-coroutines with build file build.gradle.kts file is here The contents of build.gradle.kts is - import org.jetbrains.kotlin.gradle.dsl.Coroutines import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { java kotlin("jvm") version "1.3.11" } group = "chapter2" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { compile(kotlin("stdlib-jdk8")) compile("org.jetbrains.kotlinx

Kotlin annotate receiver of extension function

*爱你&永不变心* 提交于 2020-06-16 04:46:26
问题 I would like to restrict on which constant value extension function can be called. For example function like: @IdRes fun <T : View> Int.find() = findViewById<T>(this) If this was called on real id, it's fine: R.id.someView.find<TextView>() // ok But this should make compilation error: 42.find<TextView>() // should be compile error Is annotating extension receiver supported in Kotlin? 回答1: As described in the documentation, you can use the following syntax: fun @receiver:IdRes <T : View> Int

Kotlin extension method as alias for long method name?

孤者浪人 提交于 2020-01-24 04:38:07
问题 I am working in Kotlin using a Kotlin-native library object containing a method whose .nameIsMuchTooLongAndIsStillNotClear . In a manner similar to typealias , I want to create an alias to the method, so I can refer to it as something .shortAndClear . To complicate matters slightly, these functions have several parameters, many of which have defaults that I'd prefer not to pre-process in a wrapper. After further research, it still seems like an extension function is the way to go. To use an

Why do we have functions that named componentN in Kotlin

牧云@^-^@ 提交于 2019-12-31 00:04:14
问题 I've just looked at Kotlin standard library and found some strange extension-functions called componentN where N is index from 1 to 5. There are functions for all types of primitives. For example: /** * Returns 1st *element* from the collection. */ @kotlin.internal.InlineOnly public inline operator fun IntArray.component1(): Int { return get(0) } It looks curiously for me. I'm interested in developers motives. Is it better to call array.component1() instead of array[0] ? 回答1: Kotlin has many

Kotlin - Extension for final class

我怕爱的太早我们不能终老 提交于 2019-12-24 10:58:08
问题 Is it possible to create extension of final classes like String? Like in swift it is possible to add additional methods inside a extension of final class . For an example - I would like to create a method in String extension which will tell me String have valid length for password. val password : String = mEdtPassword!!.getText().toString() // how to define haveValidLength method in extension val isValid : Boolean = password.haveValidLength() Note - That example is just for a sake to

Kotlin List tail function

删除回忆录丶 提交于 2019-12-23 06:48:37
问题 I am trying to find a tail function in List<T> but I couldn't find any. I ended up doing this. fun <T> List<T>.tail() = this.takeLast(this.size -1) Is there a better way to do this? 回答1: Kotlin doesn't have a built-in List<T>.tail() function, so implementing your own extension function is the only way. Although your implementation is perfectly fine, it can be simplified a bit: fun <T> List<T>.tail() = drop(1) Or, instead of extension function, you can define an extension property: val <T>

kotlin connect to self-signed https server

我是研究僧i 提交于 2019-12-22 11:23:17
问题 I have the following kotlin code: val urlPath = "https://10.0.2.2:8080" var data: String try { data = URL(urlPath).readText() } catch (e: Exception) { Log.e("doInBackground", "Exception caught: ${e.localizedMessage}") error = when (e) { is MalformedURLException -> "Invalid URL" is IOException -> "Network Error" else -> { "Network error: ${e.localizedMessage}" } } } If I use the above code to connect to a http server, the above code works. However when I try to connect to a https server with a

kotlin connect to self-signed https server

霸气de小男生 提交于 2019-12-22 11:20:25
问题 I have the following kotlin code: val urlPath = "https://10.0.2.2:8080" var data: String try { data = URL(urlPath).readText() } catch (e: Exception) { Log.e("doInBackground", "Exception caught: ${e.localizedMessage}") error = when (e) { is MalformedURLException -> "Invalid URL" is IOException -> "Network Error" else -> { "Network error: ${e.localizedMessage}" } } } If I use the above code to connect to a http server, the above code works. However when I try to connect to a https server with a