Swift Int() Does not recognize some types

Originator:justatheory
Number:rdar://17294989 Date Originated:2014-06-12
Status:Closed Resolved:
Product:Xcode Product Version:Version 6.0/6A215
Classification:Developer Tools Reproducible:Always
 
Summary:
This code works:

    let foo = Int(4)
    let bar = Int(0.25)
    let baz = Int(4 * 0.33)

`baz` ends up set to 1 as expected. However, using constants, something gets confused:

    let foo = 4
    let bar = 0.33
    let baz = Int(foo * bar)

Here, the attempt to set `baz` yields an error: "Could not find an overload for 'init' that accepts the supplied arguments". Would be nice if it said what the types of those arguments are, but in any event, I'm would expect the result of `foo * bar` to be a Double. And the fact that it works in the first example literal values leads me to believe that it should work for constant variables, as well.

Steps to Reproduce:
Paste this into a playground:

    let foo = 4
    let bar = 0.33
    let baz = Int(foo * bar)


Expected Results:
`baz` should be set to 1

Actual Results:
"Could not find an overload for 'init' that accepts the supplied arguments"

Version:
Xcode Version 6.0/6A215l & OS X Version 10.9.3/13D65

Comments

Comment from David Wheeler 19-Feb-2015 03:00 PM

Welcome to Swift version 1.2. Type :help for assistance. 1> let foo = 4 foo: Int = 4 2> let bar = 0.33 bar: Double = 0.33000000000000002 3> let baz = Int(foo * bar) repl.swift:3:15: error: binary operator '*' cannot be applied to operands of type 'Int' and 'Double' let baz = Int(foo * bar) ^ repl.swift:3:15: note: Overloads for '*' exist with these partially matching parameter lists: (Int, Int), (Double, Double) let baz = Int(foo * bar) ^

Hey, that's a much better error. Thank you! This issue has been verified as resolved and can be closed.

By justatheory at Feb. 19, 2015, 11:02 p.m. (reply...)

From: Apple Developer Relations 19-Feb-2015 02:25 PM

Xcode 6.3 introduces significant improvements with Swift 1.2, the latest revision to our innovative new programming language for Cocoa and Cocoa Touch, working side-by-side with Objective-C.

Please test your issue with the latest Xcode 6.3, and let us know whether you still see the same behavior. This is a pre-release version of the complete Xcode developer toolset for Mac, iPhone, iPad, and Apple Watch. This release requires OS X Yosemite.

Xcode 6.3 - Build 6D520o https://developer.apple.com/xcode/downloads/

By justatheory at Feb. 19, 2015, 11:02 p.m. (reply...)

Comment from David Wheeler

Oh, I get it. This is happening because one cannot execute an operator on values of two different types, right? That's why this does work:

let baz = Double(foo) * bar

I'm so used to numbers just working together in other languages that this confused me. I think you should consider adding support for operations on compatible numeric values, where the return value will always be the same as the operand with the largest size/highest precision.

By justatheory at June 13, 2014, 12:21 a.m. (reply...)

Comment from David Wheeler

In fact, this doesn't work, either:

let baz = foo * bar
By justatheory at June 13, 2014, 12:18 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!