Is it possible to check a value confirms a generic protocol dynamically?
I would like to do something like this:
import func Darwin.atoll
func anyToInt(a: Any) -> IntMax {
if let v = a as? IntegerType { // error!!
return v.toIntMax()
} else {
return atoll("\(a)")
}
}
This makes compile errors with a message "error: protocol 'IntegerType' can only be used as a generic constraint ...".
If I used a correct static type, I would use overloading by type-parameter constraints:
func anyToInt<T where T: IntegerType>(a: T) -> IntMax {
return a.toIntMax()
}
func anyToInt<T>(a: T) -> IntMax {
return atoll("\(a)")
}
Unfortunately, however, there's no method to employ static types instead of Any in my case.