Skip to content

Instantly share code, notes, and snippets.

View CheolhoJeon's full-sized avatar
🙏

Cheolho Jeon CheolhoJeon

🙏
View GitHub Profile
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"
package chap6.ExceptionHandling
fun function1(): Int =
throw Exception1(-52)
fun function2() = function1()
fun function3() = function2()
fun main() {
package chap6.ExceptionHandling
import atomictest.capture
class Exception1(
val value: Int
): Exception("wrong value: $value")
open class Exception2(
description: String
package chap5.CompanionObjects
import atomictest.trace
class CompanionInit {
companion object {
init {
trace("Companion Constructor")
}
}
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) }
package chap5.CompanionObjects
import atomictest.eq
interface Extended: ZI {
fun u(): String
}
class Extend : ZI by Companion, Extended {
companion object: ZI {
package chap5.CompanionObjects
import atomictest.trace
class ZIClosed : ZI {
override fun f() = "ZIClosed.f()"
override fun g() = "ZIClosed.g()"
}
class ZIDelegation {
package chap5.CompanionObjects
import atomictest.trace
interface ZI {
fun f(): String
fun g(): String
}
open class ZIOpen : ZI {
package chap5.CompanionObjects
import atomictest.eq
class Counted {
companion object {
private var count = 0
}
private val id = count++
override fun toString() = "#$id"
package chap5.CompanionObjects
import atomictest.eq
class CompanionObjectFunction {
companion object {
private var n: Int = 0
fun increment() = ++n
}
}