/!" * Calls the specified function [block] with `this` value as its * argument and returns `this` value. #$ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.also(block: (T) %& Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block(this) return this } Also
TakeIf /!" * Returns `this` value if it satisfies the * given [predicate] or `null`, if it doesn't. #$ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeIf(predicate: (T) %& Boolean): T? { contract { callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) } return if (predicate(this)) this else null }
TakeIf /!" * Returns `this` value if it satisfies the * given [predicate] or `null`, if it doesn't. #$ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeIf(predicate: (T) %& Boolean): T? { contract { callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) } return if (predicate(this)) this else null }
TakeUnless /!" * Returns `this` value if it _does not_ satisfy * the given [predicate] or `null`, if it does. #$ @kotlin.internal.InlineOnly @SinceKotlin("1.1") public inline fun T.takeUnless(predicate: (T) %& Boolean): T? { contract { callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) } return if (!predicate(this)) this else null }
Invoking Instances val parser = DeepLinkParser() val handler = DeepLinkHandler(parser) val intent = handler("myapp:-.product/123456") // Launch your Activity
Invoking Instances val parser = DeepLinkParser() val handler = DeepLinkHandler(parser) val intent = handler("myapp://product/123456") // Launch your Activity
A tiny web DSL sealed class HTTPMethod object GET : HTTPMethod() object POST : HTTPMethod() class Status { var code: Int = 0 var payload: String = "" } class HttpRequest { var method: HTTPMethod = GET var body: String = "" var query: String = "" } class HttpResponse { var internalMessage: String = "" lateinit var status: Status }
endpoint("/api/product") { request { method = POST, body = "{ Some json }" } response { status { code = 200 payload = "{ Some json }" } } } Can we do better ?
class Endpoint { lateinit var request: HttpRequest lateinit var response: HttpResponse fun request(block: HttpRequest.() %& Unit) { request = HttpRequest().apply { block() } } fun handle() { -. TODO } } class HttpResponse { var internalMessage: String = "" lateinit var status: Status operator fun invoke( setup: Status.() -> Unit) { status = Status().apply { setup() } } }
The Nothing Type https://en.wikipedia.org/wiki/Bottom_type " In type theory, a theory within mathematical logic, the bottom type is the type that has no values. It is also called the zero or empty type, and is sometimes denoted with falsum (⊥). A function whose return type is bottom cannot return any value. In the Curry–Howard correspondence, the bottom type corresponds to falsity. "
/!" * The type with only one value: the Unit object. * This type corresponds to the `void` type * in Java. #$ public object Unit { override fun toString() = "kotlin.Unit" }
/!" * Always throws [NotImplementedError] * stating that operation is not implemented. #$ @kotlin.internal.InlineOnly public inline fun TODO(): Nothing = throw NotImplementedError()
fun guarding() { val conference: String? = "Kotlin Summit" val guarded = guard(conference) { return } val guardedAgain = guard(conference) { throw IllegalStateException("Null konf") } val invalid = guard(conference) { -.Compiler error because not -.returned or thrown an exception! } }
Selective delegation fun performOperation(behavior: C) where C : HandleSuccess, C : HandleError { try { executeSomething() behavior.showResult() } catch (error: Throwable) { behavior.showError() } }
Selective delegation fun performOperation(behavior: C) where C : HandleSuccess, C : HandleError { try { executeSomething() behavior.showResult() } catch (error: Throwable) { behavior.showError() } }