This file contains 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
package chap6.ExceptionHandling | |
import atomictest.eq | |
import java.lang.Exception | |
fun toss(which: Int) = when (which) { | |
1 -> throw Exception1(1) | |
2 -> throw Exception2("Exception 2") | |
3 -> throw Exception3("Exception 3") | |
else -> "OK" |
This file contains 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
package chap6.ExceptionHandling | |
fun function1(): Int = | |
throw Exception1(-52) | |
fun function2() = function1() | |
fun function3() = function2() | |
fun main() { |
This file contains 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
package chap6.ExceptionHandling | |
import atomictest.capture | |
class Exception1( | |
val value: Int | |
): Exception("wrong value: $value") | |
open class Exception2( | |
description: String |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.trace | |
class CompanionInit { | |
companion object { | |
init { | |
trace("Companion Constructor") | |
} | |
} |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.eq | |
class Numbered2 | |
private constructor(private val id: Int) { | |
override fun toString(): String = "#$id" | |
companion object Factory { | |
fun create(size: Int) = | |
List(size) { Numbered2(it) } |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.eq | |
interface Extended: ZI { | |
fun u(): String | |
} | |
class Extend : ZI by Companion, Extended { | |
companion object: ZI { |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.trace | |
class ZIClosed : ZI { | |
override fun f() = "ZIClosed.f()" | |
override fun g() = "ZIClosed.g()" | |
} | |
class ZIDelegation { |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.trace | |
interface ZI { | |
fun f(): String | |
fun g(): String | |
} | |
open class ZIOpen : ZI { |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.eq | |
class Counted { | |
companion object { | |
private var count = 0 | |
} | |
private val id = count++ | |
override fun toString() = "#$id" |
This file contains 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
package chap5.CompanionObjects | |
import atomictest.eq | |
class CompanionObjectFunction { | |
companion object { | |
private var n: Int = 0 | |
fun increment() = ++n | |
} | |
} |
NewerOlder