kotlin-extension

How to check “instanceof ” class in kotlin?

假如想象 提交于 2019-12-09 02:24:57
问题 In kotlin class, I have method parameter as object (See kotlin doc here ) for class type T . As object I am passing different classes when I am calling method. In Java we can able to compare class using instanceof of object which class it is. So I want to check and compare at runtime which Class it is? How can I check instanceof class in kotlin? 回答1: Use is . if (myInstance is String) { ... } or the reverse !is if (myInstance !is String) { ... } 回答2: Combining when and is : when (x) { is Int

Testing extension functions inside classes

倾然丶 夕夏残阳落幕 提交于 2019-12-08 19:55:35
问题 If we want to test an extension function on a type, we can create an instance of this type, call the function and check the returned value. But what about testing extension functions defined inside classes? abstract class AbstractClass<T> { fun doStuff(): T = "Hello".foo() abstract fun String.foo(): T } class SubClass1: AbstractClass<Int>() { override fun String.foo(): Int = 1 } class SubClass2: AbstractClass<Boolean>() { override fun String.foo(): Boolean = true } How do we test the logic of

How to properly use the URL with Kotlin Android

笑着哭i 提交于 2019-12-08 08:41:55
问题 I want to use override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val json = URL("https://my-api-url.com/something").readText() simpleTextView.setText(json) } but this fatal error occurs FATAL EXCEPTION: main Process: com.mypackage.randompackage, PID: 812 java.lang.RuntimeException: Unable to start activity ComponentInfo{ ***.MainActivity}: android.os.NetworkOnMainThreadException How can I simply read a JSON from a URL

Kotlin: Possible to modify functions during compile time through metaprogramming?

a 夏天 提交于 2019-12-07 05:56:59
问题 In dynamic languages like JavaScript/Python, it's possible to overwrite or "modify" functions during run-time. For example, in order to modify the alert function in JS, one could do: const _prev_alert = window.alert; window.alert = function() { _prev_alert.apply(this, arguments); console.log("Alert function was called!"); } This would output "Alert function was called!" to the console every time the alert function is called. Now, obviously something like this would be impossible during

How to properly use the URL with Kotlin Android

梦想与她 提交于 2019-12-06 16:39:36
I want to use override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val json = URL("https://my-api-url.com/something").readText() simpleTextView.setText(json) } but this fatal error occurs FATAL EXCEPTION: main Process: com.mypackage.randompackage, PID: 812 java.lang.RuntimeException: Unable to start activity ComponentInfo{ ***.MainActivity}: android.os.NetworkOnMainThreadException How can I simply read a JSON from a URL link? The package of the async function doesn't exist. Android doesn't allow accessing the internet

Split space from string not working in Kotlin

試著忘記壹切 提交于 2019-12-05 14:31:29
问题 Anyone wonder this ? Splitting SPACE (" ") in kotlin is not working, i tried with different regex codes but is not working at all. Tried with this : value.split("\\s")[0]; value.split("\\s+")[0]; value.split("\\s++")[0]; Then i came up with solution -> Create java constant class which contains this function and returns string array to your kotlin class. Is there any other solution for this problem where we can directly achieve this thing? Solution : As @Edson Menegatti said : KOTLIN Specific

Kotlin: Possible to modify functions during compile time through metaprogramming?

限于喜欢 提交于 2019-12-05 09:13:49
In dynamic languages like JavaScript/Python, it's possible to overwrite or "modify" functions during run-time. For example, in order to modify the alert function in JS, one could do: const _prev_alert = window.alert; window.alert = function() { _prev_alert.apply(this, arguments); console.log("Alert function was called!"); } This would output "Alert function was called!" to the console every time the alert function is called. Now, obviously something like this would be impossible during runtime in Kotlin-JVM or Kotlin-Native due to their static nature. However, in regards to those same

Split space from string not working in Kotlin

僤鯓⒐⒋嵵緔 提交于 2019-12-04 02:27:30
Anyone wonder this ? Splitting SPACE (" ") in kotlin is not working, i tried with different regex codes but is not working at all. Tried with this : value.split("\\s")[0]; value.split("\\s+")[0]; value.split("\\s++")[0]; Then i came up with solution -> Create java constant class which contains this function and returns string array to your kotlin class. Is there any other solution for this problem where we can directly achieve this thing? Solution : As @Edson Menegatti said : KOTLIN Specific : WORKING values.split("\\s".toRegex())[0] Many people suggested this solution : NOT WORKING values

Kotlin Android debounce

你。 提交于 2019-12-03 12:03:28
Is there any fancy way to implement debounce logic with Kotlin Android? I'm not using Rx in project. There is a way in Java , but it is too big as for me here. You can use kotlin coroutines to achieve that. Here is an example . Be aware that coroutines are experimental at kotlin 1.1+ and it may be changed in upcoming kotlin versions. UPDATE Since Kotlin 1.3 release, coroutines are now stable. Thanks to https://medium.com/@pro100svitlo/edittext-debounce-with-kotlin-coroutines-fd134d54f4e9 and https://stackoverflow.com/a/50007453/2914140 I wrote this code: private var textChangedJob: Job? = null

Kotlin extension clash

北城余情 提交于 2019-12-01 03:58:07
If I have a jar, on the classpath, where I've created an extension function on say the String class for argument's sake and I have another jar with the same extension function on String, how will Kotlin resolve the two? I presume if both functions are defined in the same packages then there will be a clash? But if different packages, how I can distinguish the two extensions? Indeed, if they're in the same package, it won't compile. For the other scenario, let's say you have two files with two different packages, containing extension functions with the same signature: First file: package ext1