Generic Type
struct Color<T> {
var red: T
var green: T
var blue: T
}
let c = Color(red: 128, green: 80, blue: 200)
// Color<Int> 형식이기 때문에 에러
// c = Color(red: 128.0, green: 80.0, blue: 200.0)
let d: Color = Color(red: 128.0, green: 80.0, blue: 200.0)
let arr: Array<Int>
let dict : Dictionary<String, Double>
extension Color where T == Int {
func getComponents() -> [T] {
return [red, green, blue]
}
}
let intColor = Color(red: 1, green: 2, blue: 3)
intColor.getComponents()
// T == Int 에 false기 때문에 에러
// let dblColor = Color(red: 1.0, green: 20.0, blue: 3.0)
// dblColor.getComponents()