Flatmap initializer inconsistency

Hi,

I'm seeing a weird issue with using an initializer in flatMap. Here's
an example:

let time: TimeInterval? = 662.82582598600004
let intTimeFlatmap = time.flatMap(Int.init) // nil
let intTime = Int(time!) // 662

I would expect for the flatMap call to return an optional Int with the
proper value of 662. Is there something I'm misunderstanding, or is
this a swift bug?

Thanks,
Halen

In my test, compiler thought you use `init?(exactly value: Double)`, which
returns nil. So this is not a bug.

Zhaoxin

···

On Tue, May 2, 2017 at 1:39 AM, Halen Wooten via swift-users < swift-users@swift.org> wrote:

Hi,

I'm seeing a weird issue with using an initializer in flatMap. Here's
an example:

let time: TimeInterval? = 662.82582598600004
let intTimeFlatmap = time.flatMap(Int.init) // nil
let intTime = Int(time!) // 662

I would expect for the flatMap call to return an optional Int with the
proper value of 662. Is there something I'm misunderstanding, or is
this a swift bug?

Thanks,
Halen
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Well, the question still remains about why the compiler chose init(exactly:) over init(). Shouldn’t there at least a warning of ambiguity?

Saagar Jha

···

On May 1, 2017, at 12:11, Zhao Xin via swift-users <swift-users@swift.org> wrote:

In my test, compiler thought you use `init?(exactly value: Double <>)`, which returns nil. So this is not a bug.

Zhaoxin

On Tue, May 2, 2017 at 1:39 AM, Halen Wooten via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Hi,

I'm seeing a weird issue with using an initializer in flatMap. Here's
an example:

let time: TimeInterval? = 662.82582598600004
let intTimeFlatmap = time.flatMap(Int.init) // nil
let intTime = Int(time!) // 662

I would expect for the flatMap call to return an optional Int with the
proper value of 662. Is there something I'm misunderstanding, or is
this a swift bug?

Thanks,
Halen
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

That is because for `Optional` the `flatMap` is `func flatMap<U>(_
transform: (Wrapped) throws -> U?) rethrows -> U?` and for `Int`, only
`init?(exactly value: Double)` fits the situation. `init(_ value: Double)`
doesn't return nil. So there is no ambiguity.

Zhaoxin

···

On Tue, May 2, 2017 at 4:20 AM, Saagar Jha <saagar@saagarjha.com> wrote:

Well, the question still remains about why the compiler chose
init(exactly:) over init(). Shouldn’t there at least a warning of ambiguity?

Saagar Jha

On May 1, 2017, at 12:11, Zhao Xin via swift-users <swift-users@swift.org> > wrote:

In my test, compiler thought you use `init?(exactly value: Double)`, which
returns nil. So this is not a bug.

Zhaoxin

On Tue, May 2, 2017 at 1:39 AM, Halen Wooten via swift-users < > swift-users@swift.org> wrote:

Hi,

I'm seeing a weird issue with using an initializer in flatMap. Here's
an example:

let time: TimeInterval? = 662.82582598600004
let intTimeFlatmap = time.flatMap(Int.init) // nil
let intTime = Int(time!) // 662

I would expect for the flatMap call to return an optional Int with the
proper value of 662. Is there something I'm misunderstanding, or is
this a swift bug?

Thanks,
Halen
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Yep, that explanation makes sense to me. It's picking the one that fits exactly.

Thanks for the help, everyone.

Halen

···

On Mon, May 1, 2017 at 11:48 PM, Zhao Xin <owenzx@gmail.com> wrote:

That is because for `Optional` the `flatMap` is `func flatMap<U>(_
transform: (Wrapped) throws -> U?) rethrows -> U?` and for `Int`, only
`init?(exactly value: Double)` fits the situation. `init(_ value: Double)`
doesn't return nil. So there is no ambiguity.

Zhaoxin

On Tue, May 2, 2017 at 4:20 AM, Saagar Jha <saagar@saagarjha.com> wrote:

Well, the question still remains about why the compiler chose
init(exactly:) over init(). Shouldn’t there at least a warning of ambiguity?

Saagar Jha

On May 1, 2017, at 12:11, Zhao Xin via swift-users <swift-users@swift.org> >> wrote:

In my test, compiler thought you use `init?(exactly value: Double)`, which
returns nil. So this is not a bug.

Zhaoxin

On Tue, May 2, 2017 at 1:39 AM, Halen Wooten via swift-users >> <swift-users@swift.org> wrote:

Hi,

I'm seeing a weird issue with using an initializer in flatMap. Here's
an example:

let time: TimeInterval? = 662.82582598600004
let intTimeFlatmap = time.flatMap(Int.init) // nil
let intTime = Int(time!) // 662

I would expect for the flatMap call to return an optional Int with the
proper value of 662. Is there something I'm misunderstanding, or is
this a swift bug?

Thanks,
Halen
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users