Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide an integer returned from a function by a double value?

Tags:

swift

Given this function:

func anInt() -> Int {
  return 5
}

This doesn't work:

anInt() / Double(2)
>> ERROR: Could not find an overload for '/' that accepts the supplied arguments

However this works:

5 / Double(2)
>> 2.5
like image 705
vithun Avatar asked May 08 '26 00:05

vithun


1 Answers

When you call anInt() / Double(2) the compiler knows you are trying divide an Int by a Double and doesn't allow that, but when you call 5 / Double(2), it can infer the type of 5 as a Double since you are dividing it by a Double.

like image 140
Connor Avatar answered May 11 '26 00:05

Connor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!