Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save creaaa/3e59bced287e829ff7ec02f11b67d251 to your computer and use it in GitHub Desktop.
Save creaaa/3e59bced287e829ff7ec02f11b67d251 to your computer and use it in GitHub Desktop.
悩ましい
/*
class Man {}
class SuperMan: Man {}
let m = Man()
let n = m as! SuperMan // これは当然無理だ。わかる。だが
*/
let m = Optional.some(42)
let n = m as! Int // なんでこのサブタイピング関係は通るんだァァァァ!!!!!
print(n) // 42
@creaaa
Copy link
Author

creaaa commented Apr 11, 2018

まさか...と思って、

let a: [Animal] = []
a as! [Cat]

やったら....やっぱこれも成功した。これもOptionalの場合と同じくらいキモい。

だって、Animal() as! Cat は当然死ぬんだぞ。Arrayの型パラになった瞬間セーフになるのは元来ちょっとおかしな話で。
...でも、
let a = String as! [Bool] も成功した。これまじどうなってんだよwww

// なんか [] が表示されない、ここ。おかしい。editで見てみて
Dog as! [Animal] だろうが、
Dog as! [Int] だろうが、やりたい放題。

とにかく、Arrayにも「コンパイラによる特別扱い」がされているのは間違いない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment