I'm trying to write some generic numerical code in Swift. Here's an example:
func linstep<X>(lambda: X, a: X, b: X) -> X {
return (X(1)-lambda)*a+lambda*b
}
That results in 'X' cannot be constructed because it has no accessible initializers
. I need to ensure that 1 can be converted to X.
So I tried this variant:
func linstep<X: IntegerLiteralConvertible>(lambda: X, a: X, b: X) -> X {
return (X(integerLiteral: 1)-lambda)*a+lambda*b
}
But that failed also.
How should I write my function?
(I know linear interpolation can be algebraically rearranged to eliminate the use of 1. But that's not the point of my question.)