natpryce
03/31/2019, 11:26 PMpablisco
04/01/2019, 6:38 AMnatpryce
04/01/2019, 8:27 AMnatpryce
04/01/2019, 8:35 AMpablisco
04/01/2019, 10:33 AMnatpryce
04/01/2019, 3:00 PMnatpryce
04/01/2019, 3:00 PMpablisco
04/02/2019, 4:15 PMInterfaces are for when you want the implementation to vary without affecting client codeNot quite right, interfaces are a way to abstract a contract between different parties (internal or external). If we had something like this:
sealed interface Post {
interface ImagePost : Post {
val image: Image
}
interface TextPost : Post {
val text: Text
}
interface LinkPost : Post {
val link: Link
}
}
Then, when consuming this, potentially from a “renderer”, we can do:
fun render(post: Post) {
when(post) {
is ImagePost -> renderImage(post.image)
is TextPost -> renderText(post.text)
is LinkPost -> renderLink(post.link)
}
}
Without knowing how they are implemented on the other side.natpryce
04/02/2019, 4:17 PMnatpryce
04/02/2019, 4:20 PMsealed class Post
abstract class ImagePost : Post() {
abstract val image: Image
}
abstract class TextPost : Post() {
abstract val text: Text
}
abstract class LinkPost : Post() {
abstract val link: Link
}
pablisco
04/02/2019, 4:20 PMnatpryce
04/02/2019, 4:21 PMpablisco
04/02/2019, 4:22 PMpablisco
04/23/2019, 7:00 AMsealed types provide a means for declaring classes and interfaces that can restrict who their subtypes are.