Skip to content

Instantly share code, notes, and snippets.

@tarunon
Last active November 7, 2017 05:30
Show Gist options
  • Save tarunon/16c2416f8d164ef8dbefbb2934108f87 to your computer and use it in GitHub Desktop.
Save tarunon/16c2416f8d164ef8dbefbb2934108f87 to your computer and use it in GitHub Desktop.
class Animal {}
class Cat: Animal {}
// Rule A
// Cat is subtype of Animal
do {
let cat: Cat = Cat()
let animal: Animal = cat
}
// Rule B
// Int is subtype of Int?
do {
let int: Int = 0
let intOptional: Int? = int // Int?.some(0)
}
// Rule C
// Optional has covariance
do {
let catOptional: Cat? = Cat?.none
let animalOptional: Animal? = catOptional // Animal?.none
}
// Question
// What happen replace `Cat : Animal` to `Int : Int?`
do {
let intOptional: Int? = Int?.none
let intOptionalOptional: Int?? = intOptional // Rule B: Int??.some(.none), Rule C: Int??.none
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment