Skip to content

Instantly share code, notes, and snippets.

@mapyo
Created April 19, 2018 22:47
Show Gist options
  • Save mapyo/0351987ef26fbe0a587b00afb17eeee1 to your computer and use it in GitHub Desktop.
Save mapyo/0351987ef26fbe0a587b00afb17eeee1 to your computer and use it in GitHub Desktop.
seald class sample
package jp.co.hoge
import org.junit.Test
sealed class Event {
class Loading : Event()
class Loaded(val result: String) : Event()
class LoadFailed(val reason: String) : Event()
}
class SampleTest {
@Test
fun sample() {
val event1 = Event.Loading()
val event2 = Event.Loaded("1です")
val event3 = Event.LoadFailed("やんごとなき理由です")
val events = listOf(event1, event2, event3)
events.forEach { hoge(it) }
}
fun hoge(event: Event) {
when (event) {
is Event.Loading -> println("読み込み中です")
is Event.Loaded -> println("こんな感じの結果です ${event.result}")
is Event.LoadFailed -> println("残念ですが失敗しました ${event.reason}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment