2

I have a function which accept values which conform to Numeric protocol, in some point I want convert it to Int, Double or CGFloat.

the error from xCode:

Initializer 'init(_:)' requires that 'T' conform to 'BinaryInteger'

my function:

func test<T: Numeric>(value1: T, value2: T) {

    let someWork = value1 + value2

    let intValue: Int = Int(someWork)               // <<: Here!
    let doubleValue: Double = Double(someWork)      // <<: Here!
    let cgflotValue: CGFloat = CGFloat(someWork)    // <<: Here!

    print(intValue, doubleValue, cgflotValue)

}

then I saw the xCode error and updated my function to this down version, I put out Numeric because BinaryInteger and BinaryFloatingPoint conform to Numeric

func test<T: BinaryInteger & BinaryFloatingPoint>(value1: T, value2: T) {
   
    let someWork = value1 + value2
    
    let intValue: Int = Int(someWork)               // <<: Here: I add BinaryInteger because of Int
    let doubleValue: Double = Double(someWork)      // <<: Here: I add BinaryFloatingPoint because of Double
    let cgflotValue: CGFloat = CGFloat(someWork)    // <<: Here: I add BinaryFloatingPoint because of CGFloat
    
    print(intValue, doubleValue, cgflotValue)
    
}

use case:

let testValue1: Int = 2
test(value1: testValue1, value2: testValue1)

xCode error for use case:

Global function 'test(value1:value2:)' requires that 'Int' conform to 'BinaryFloatingPoint'

use case:

let testValue2: Double = 2.0
test(value1: testValue2, value2: testValue2)

xCode error for use case:

Global function 'test(value1:value2:)' requires that 'Double' conform to 'BinaryInteger'

How can I make Int conform to BinaryFloatingPoint?

Or:

How can I make Double/CGFloat conform to BinaryInteger?

as you see in xCode errors.

ios coder
  • 1
  • 4
  • 31
  • 91
  • 1
    Possibly helpful: https://stackoverflow.com/a/53774667/1187415 – Martin R Jul 09 '21 at 11:41
  • 1
    So judging by the accepted answer I guess that you never want to mix integers and floating point values as arguments to the function? – Joakim Danielson Jul 09 '21 at 12:11
  • @JoakimDanielson: I never wanted to mix them, I wanted my function cover all kind of numbers. the errors forced me to add those protocols beside together. – ios coder Jul 09 '21 at 12:23

2 Answers2

3

You can add two same functions with different constraint

//T: BinaryInteger & BinaryFloatingPoint
func test<T>(value1: T, value2: T) where T: BinaryInteger{
    
    let someWork = value1 + value2
    
    let intValue: Int = Int(someWork)               // <<: Here: I add BinaryInteger because of Int
    let doubleValue: Double = Double(someWork)      // <<: Here: I add BinaryFloatingPoint because of Double
    let cgflotValue: CGFloat = CGFloat(someWork)    // <<: Here: I add BinaryFloatingPoint because of CGFloat
    
    print(intValue, doubleValue, cgflotValue)
    
}
func test<T>(value1: T, value2: T) where T: BinaryFloatingPoint{
    
    let someWork = value1 + value2
    
    let intValue: Int = Int(someWork)               // <<: Here: I add BinaryInteger because of Int
    let doubleValue: Double = Double(someWork)      // <<: Here: I add BinaryFloatingPoint because of Double
    let cgflotValue: CGFloat = CGFloat(someWork)    // <<: Here: I add BinaryFloatingPoint because of CGFloat
    
    print(intValue, doubleValue, cgflotValue)
    
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
0

You can't. An integer cannot conform to BinaryFloatingPoint and a floating point number cannot conform to BinaryInteger, and you can't make them.

Your function test wants an argument that conforms to both. Int doesn't and Double doesn't.

gnasher729
  • 51,477
  • 5
  • 75
  • 98