So I have a method that has 3 different types of arguments that could come in:
Int32
, Int
and Double
. So the idea was to use generics to minimize the interface
func resetProgressBarChunks<T:Numeric>(originalIterationCount: T) {
guard let iCount = originalIterationCount as? Double else {return}
But what I have realized, is at runtime, the Int32
and Int
arguments will actually fail that guard let
. It makes sense, it was just wishful thinking on my part.
But if I try to simply cast a Numeric
into a double, the compiler will bark:
func resetProgressBarChunks<T:Numeric>(originalIterationCount: T) {
guard let iCount = Double(originalIterationCount) else {return}
Cannot invoke initializer for type 'Double' with an argument of type '(T)'
Which I suppose also makes sense, because there is no initializer for Double that takes a Generic.
So it looks like I'm about to be forced to write 3 methods with different parameter types. The Int32
and Int
parameter types would just cast into a Double
and then call the Double
method. Is this really the best way? I really was hoping I could leverage Numeric
somehow