This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Disposable { | |
| fun dispose() | |
| } | |
| fun using(value : Disposable, body : ()->Unit) { | |
| try { | |
| body() | |
| } finally { | |
| value.dispose() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Event<T> { | |
| private val handlers = arrayListOf<(Event<T>.(T) -> Unit)>() | |
| fun plusAssign(handler: Event<T>.(T) -> Unit) { handlers.add(handler) } | |
| fun invoke(value: T) { for (handler in handlers) handler(value) } | |
| } | |
| val e = Event<String>() // define event | |
| fun main(args : Array<String>) { | |
| e += { println(it) } // subscribe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class IBAN(val country: String, val account : String) | |
| // parseIBAN returns nullable IBAN | |
| fun parseIBAN(code : String) : IBAN? { | |
| ... | |
| } | |
| val iban = parseIBAN(userInput) | |
| when (iban) { | |
| null -> throw InvalidIBANException() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| when(v) { | |
| is Vector -> // here v is of type Vector | |
| is Versor -> // here v is of type Versor | |
| else -> throw UnsupportedTypeException() | |
| } |
NewerOlder