I need to write an initializer for any FloatingPoint
value, which can take other type of FloatingPoint
, and convert it to Self
. Is this possible in swift
? I tried something like this:
init<FloatingPointType: FloatingPoint>(_ value: FloatingPointType) {
guard let sameValue = value as? Self else {
// Pass throu double to get best precision
self.init(Double(value))
return
}
self = sameValue // The same type
}
But this just creates infinite loop. The problem is with creating new self, there is no initializer that takes any other FloatingPoint
. I think this might be impossible, but maybe there is some way to achieve this.