Kotlin is nullsafe, but there are things to keep in mind when using Kotlin from Java. Kotlin has an implementation that accepts it as T! Type called Platform types. For example, it is as follows. fun getString(key:String, defValue:String) = sharedPref.getString(key:String, defValue:String) // platform types. return value is T! fun getString(key:String, defValue:String): String? = sharedPref.getString(key:String, defValue:String) // Declare it to be nullable The above is a simplified method of SharedPreferences#getString. but SharedPreferences#getString returns @Nullable String.
Validation of Kotlin function is very useful. I will explain the semantic validation of Kotlin. Argument validaiton require require checks arguments. Seeing the following sources shows you throwing IllegalArgumentException @kotlin.internal.InlineOnly public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit { if (!value) { val message = lazyMessage() throw IllegalArgumentException(message.toString()) } } State validation check check checks states. Seeing the following sources shows you throwing IllegalStateException @kotlin.internal.InlineOnly public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit { if (!value) { val message = lazyMessage() throw IllegalStateException(message.toString()) } } Assertion validation assert An assertion is described as a row and serves as a checkpoint.
stdlib / kotlin / Nothing kotlin/core/builtins/native/kotlin/Nothing.kt public class Nothing private() {} Nothing has no instances. You can use Nothing to represent “a value that never exists”. for example. if a function has the return type of Nothing. it means that it never returns. How to use When you represent a null is should I use Nothing. >>> fun crash(x:Nothing) {} >>> crash("foo") error: type mismatch: inferred type is String but
I tried to learn the design pattern. I will try to write a sample in kotlin. Iterator Adapter TemplateMethod FactoryMethod Singleton Prototype Builder AbstractFactory Bridge Strategy Composite Decorator Visitor Chain of Responsibility Facade Mediator Observer Memento State Flyweight Proxy Command Interpreter
What is enclosingClass. https://github.com/MicroUtils/kotlin-logging/blob/5c297178d7edcae6af2e248328c42dad11adc88c/src/main/kotlin/mu/internal/KLoggerNameResolver.kt inline private fun <T: Any> unwrapCompanionClass(ofClass: Class<T>): Class<*> = if (ofClass.enclosingClass != null && ofClass.enclosingClass.kotlin.companionObject?.java == ofClass) { ofClass.enclosingClass } else { ofClass }
The following code is nullpointer exception. private val userLumps by lazy { intent.getSerializableExtra(ARG_USER_LUMP) as ArrayList<UserLump> } private val adapter = OriginalAdapter(userLumps) Error details Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference It becomes null before it is initialized wit the lifecycle proble. No proble if set in onCreate.
Generics code of java was expressed using the reified of kotlin. # Java public <T extends BaseCode> T getCode(Class<T> type) { return (T) codes.get(type); } # Kotlin inline fun <reified T : BaseCode> Code.system() = getCode(T::class.java)
The following error occurred in the android studio. Resolve Error SDK location not found. Define location with sdk.dir in the local.properties files or with an ANDROID_HOME environment variable. I do the following in the corresponding you in my case. 1. Create local.properties 2. Add local.properties to sdk.dir=/Users/xxx/Downloads/android-sdk-macosx 3. set ANDROID_HOME=/Users/xxx/Downloads/android-sdk-macosx/sdk 4. export ANDROID_HOME=/Users/xxx/Downloads/android-sdk-macosx/sdk 5. ./gradlew 6. reinstall android studio It works.
Implementation of the fragmnet on fragment. val transaction = fragmentManager.beginTransaction() transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) transaction.replace(R.id.placeholder, fragment).addToBackStack(null).commit()
The animation implementation of drag-and-drop I wrote in kotlin. This is a very easy and simple. // from view fromView.setOnTouchListener(View.OnTouchListener { view, event -> if (event.getAction() == MotionEvent.ACTION_DOWN) { val data = ClipData.newPlainText("", "") view.startDrag(data, View.DragShadowBuilder(view), view, View.DRAG_FLAG_GLOBAL) view.setVisibility(View.INVISIBLE) return@OnTouchListener true } else { return@OnTouchListener false } }) // to view toView.setOnDragListener(View.OnDragListener { view, event -> if (event.action == DragEvent.ACTION_DROP) { // do something for drop (event.localState as View).visibility =