Skip to content

Instantly share code, notes, and snippets.

View adam-arold's full-sized avatar
💭
Exploring the n'th dimension.

Adam Arold adam-arold

💭
Exploring the n'th dimension.
View GitHub Profile
object Positions {
@JvmStatic
@JvmOverloads
fun create(x: Int,
y: Int = x) = Position.create(x, y)
}
interface Position {
val x: Int
val y: Int
fun calculateArea(): Int = x.times(y)
companion object {
@JvmOverloads
open class PositionBase(
override val x: Int,
override val y: Int) : Position {
final override fun calculateArea(): Int {
return super.calculateArea()
}
}
abstract class PositionBase : Position
interface Position {
val x: Int
val y: Int
fun calculateArea(): Int = x.times(y)
}
class EventBus {
@JvmName("subscribe")
internal fun <T: Event> subscribe(eventType: Class<T>) {
}
}
// reified is not visible from Java
class EventBus {
inline fun <reified T : Event> subscribe(
noinline callback: (T) -> Unit) {
// ...
}
}
interface Position {
val x: Int
val y: Int
}
// idiomatic in Kotlin
pos.x
pos.y
fun usage() {
EventBus().subscribe<Event> { }
}
inline fun <reified T : Event> EventBus.subscribe(
noinline callback: (T) -> Unit) {
return subscribe(
klass = T::class,
callback = callback)
}