Do Int and Double share a common parent class in Swift
Asked Answered
C

2

11

I'm wondering if there is an easier way to write these two initializers as a generic Initializer

public required init(_ value : Double) {
    super.init(value: value, unitType: unit)
}

public required init(_ value : Int) {
    let v = Double(value)
    super.init(value: v, unitType: unit)
}

Something like:

public init<T>(_value : T) {
    let v = Double(T)
    super.init(value: v, unitType: unit)
}

(which of course doesn't compile)

I've looked through the code of both Int and Double and am missing any real thing that ties them together.

Copeck answered 28/5, 2015 at 13:25 Comment(3)
They share many protocols in common – they're both AbsoluteValuable, Comparable, IntegerLiteralConvertible. Check out SwiftDoc to get an easy look at the full hierarchy. But to really answer this question requires more info about the class you are initializing – it really depends on what value: in super.init is.Darmstadt
It requires a Double to initialize, however, I figured I could wrap any class (including) double in a Double and it should work.Copeck
Int and Double are structs, not classesBoresome
L
14

Take a look at the Swift header:

extension String : StringInterpolationConvertible {
    init(stringInterpolationSegment expr: String)
    init(stringInterpolationSegment expr: Character)
    init(stringInterpolationSegment expr: UnicodeScalar)
    init(stringInterpolationSegment expr: Bool)
    init(stringInterpolationSegment expr: Float32)
    init(stringInterpolationSegment expr: Float64)
    init(stringInterpolationSegment expr: UInt8)
    init(stringInterpolationSegment expr: Int8)
    init(stringInterpolationSegment expr: UInt16)
    init(stringInterpolationSegment expr: Int16)
    init(stringInterpolationSegment expr: UInt32)
    init(stringInterpolationSegment expr: Int32)
    init(stringInterpolationSegment expr: UInt64)
    init(stringInterpolationSegment expr: Int64)
    init(stringInterpolationSegment expr: UInt)
    init(stringInterpolationSegment expr: Int)
}

Similarly:

func +(lhs: UInt8, rhs: UInt8) -> UInt8
func +(lhs: Int8, rhs: Int8) -> Int8
func +(lhs: UInt16, rhs: UInt16) -> UInt16
func +(lhs: Int16, rhs: Int16) -> Int16
func +(lhs: UInt32, rhs: UInt32) -> UInt32
func +(lhs: Int32, rhs: Int32) -> Int32
func +(lhs: UInt64, rhs: UInt64) -> UInt64
func +(lhs: Int64, rhs: Int64) -> Int64
func +(lhs: UInt, rhs: UInt) -> UInt
func +(lhs: Int, rhs: Int) -> Int
func +(lhs: Float, rhs: Float) -> Float
func +(lhs: Double, rhs: Double) -> Double
func +(lhs: Float80, rhs: Float80) -> Float80

If it were possible to write one generic function for all those different numeric types, they would have done it, surely. So the answer to your question must be No.

(And in any case they can hardly share a parent class, as they are not classes. They are structs.)

Now, of course, if only Int and Double are in question, you could extend Int and Double to adopt a common protocol and make that protocol the expected type...

Levorotation answered 28/5, 2015 at 13:33 Comment(4)
This is a misleading answer. It depends on the use case. While these types require their own addition operator, it is perfectly possible to define broad classes of operation across, say, all the integers, such as for example func sum<S: SequenceType where S.Generator.Element: IntegerType>(seq: S) -> S.Generator.Element { return reduce(seq,0) { $0 + $1 } }. That said, there are few protocols that bridge between both integers and floats.Darmstadt
@AirspeedVelocity Actually I was just waiting for you to come along and give the right answer. Please give it!Levorotation
FYI: As of Swift 4.0 this protocol is deprecated.Disturbed
@KirillTitov Does that mean one can not creae a string from a numeric type now?Thinking
D
2

You can use the type SignedNumeric with the any keyword.

An example for Swift 5.9:

let numbers: [any SignedNumeric] = [
  Double(0.0),
  Int(0),
]

for number in numbers {
  if let aDouble = number as? Double {
    print ("double value \(aDouble)")
  } else {
    print ("int value \(number)")
  }
}
Dordogne answered 27/12, 2023 at 14:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.