Hi Guys, the following example:
let a:Int = 2;
let b:Float=2.0
let c:String="2.0"
func typeOfVal(_ a:Int)->Void{
print(type(of:a))
}
func typeOfVal(_ a:Float)->Void{
print("Float")
}
func typeOfVal<T>(_ a:T)->Void{
print("Generic")
}
func typeOfValIndirect<T>(_ a:T)->Void{
typeOfVal(a)
}
typeOfVal(a)
typeOfVal(b)
typeOfVal(c)
typeOfValIndirect(a)
typeOfValIndirect(b)
typeOfValIndirect(c)
When executing this on repl.it/languages/swift I get the following:
swiftc -o main main.swift
./main
Int
Float
Generic
Generic
Generic
Generic
While the first three calls are ok, the latter are not.
I know that the designers of swift had their reasons to support only local specialization, but why we just couldn't override this behavior in these cases which certainly causes specialization to be a bit slower but gives us the desired behavior?
What about an annotation like @globalSpecialization
over the typeOfVal
functions?
See also: Compile-Time Generic Specilization