Created
February 14, 2018 09:35
-
-
Save jmfayard/080965117e02f7e96a6484953e57dccb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Producer<out T : Beverage>( | |
val beverage: T | |
) { | |
fun produce() : T = beverage | |
} | |
class Consumer<in T: Beverage> { | |
fun consume(t: T) = println("Thanks for the drink $t!") | |
} | |
interface Beverage | |
object Coffee : Beverage | |
object Whisky : Beverage | |
fun main(args: Array<String>) { | |
val colombia: Producer<Coffee> = Producer(Coffee) | |
val scottland: Producer<Whisky> = Producer(Whisky) | |
val noCoffeeThere : Coffee = scottland.produce() // error | |
val beverages: List<Beverage> = listOf(colombia, scottland).map { it.produce() } | |
val starbucks = Consumer<Coffee>() | |
starbucks.consume(colombia.produce()) | |
starbucks.consume(scottland.produce()) // error | |
val pub = Consumer<Whisky>() | |
pub.consume(scottland.produce()) | |
pub.consume(colombia.produce()) // error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment