Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Created April 21, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/cb58213b9a78f74927d3 to your computer and use it in GitHub Desktop.
Save norio-nomura/cb58213b9a78f74927d3 to your computer and use it in GitHub Desktop.
#Swift ジェネリックでは無いクラスのジェネリックなメソッドを、ジェネリックなサブクラスからオーバーライド出来ない罠にはまった。
class C {
func f<T>(v: T) -> String {
return "C(\(v))"
}
func callf() -> String{
return f("test")
}
}
class D<U>: C {
func f<T>(v: T) -> String {// swift compiler never warn missing 'override' keyword, and not allow placing 'override' keyword.
return "D(\(v))"
}
}
let d = D<String>()
d.callf() // "C(test)"
@norio-nomura
Copy link
Author

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