raulraja
05/17/2018, 9:52 PMjacob
05/21/2018, 12:02 AMjacob
05/21/2018, 12:02 AM@higherkind
sealed class L<A> : LOf<A> {
class Empty<A> : L<A>()
data class Cons<A>(val head: A, val tail: L<A>) : L<A>()
companion object
}
and want to define a show instance for it that will use the show instance for A to print itsimon.vergauwen
05/21/2018, 8:14 AMsimon.vergauwen
05/21/2018, 8:14 AMjacob
05/21/2018, 8:15 AMjacob
05/21/2018, 8:15 AMjacob
05/21/2018, 8:16 AMsimon.vergauwen
05/21/2018, 8:16 AM@instance(L::class)
instance LShowInstance<A> : Show<L<A>> {
fun S(): Show<A> //dependeny on Show<A>
fun L<A>.show() = l.joinToString { S().run { it.show() } }
}
simon.vergauwen
05/21/2018, 8:16 AMsimon.vergauwen
05/21/2018, 8:16 AMjacob
05/21/2018, 8:16 AMsimon.vergauwen
05/21/2018, 8:17 AMjacob
05/21/2018, 8:17 AMjacob
05/21/2018, 8:17 AM@higherkind
sealed class O<A>: OOf<A> {
class None<A>: O<A>()
class Some<A>(val t: A): O<A>()
companion object
}
@instance(O::class)
interface OShowInstance<A> : Show<OOf<A>> {
fun SA(): Show<A>
override fun OOf<A>.show(): String {
val x = this.fix()
return when (x) {
is O.None -> "None"
is O.Some -> "Some(${SA().run { x.t.show() }})"
}
}
}
fun main(args: Array<String>) {
O.show<O<Int>>(O.show(Int.show())).run {
O.Some(O.Some(1)).show().let(::println)
}
}
jacob
05/21/2018, 8:18 AMsimon.vergauwen
05/21/2018, 8:19 AMpredef.kt
simon.vergauwen
05/21/2018, 8:19 AMjacob
05/21/2018, 8:19 AMsimon.vergauwen
05/21/2018, 8:19 AMjacob
05/21/2018, 8:19 AMjacob
05/21/2018, 8:19 AMsimon.vergauwen
05/21/2018, 8:20 AMjacob
05/21/2018, 8:20 AMsimon.vergauwen
05/21/2018, 8:20 AMjacob
05/21/2018, 8:20 AMjacob
05/21/2018, 8:21 AMsimon.vergauwen
05/21/2018, 8:21 AMjacob
05/21/2018, 8:22 AMsimon.vergauwen
05/21/2018, 8:26 AM