Is it possible to check a value that dynamically confirms a common protocol?
I would like to do something like this:
import func Darwin.atoll
func anyToInt(a: Any) -> IntMax {
if let v = a as? IntegerType {
return v.toIntMax()
} else {
return atoll("\(a)")
}
}
This makes compilation errors with the message "error: IntegerType protocol" can only be used as a general restriction ... ".
If I used the correct static type, I would use overloading according to the restrictions of the type parameters:
func anyToInt<T where T: IntegerType>(a: T) -> IntMax {
return a.toIntMax()
}
func anyToInt<T>(a: T) -> IntMax {
return atoll("\(a)")
}
Unfortunately, however, there is no way to use static types instead of Any in my case.
source
share