ambiguous minus operator


(J.E. Schotsman) #1

Hello,

I am trying to define an operator that subtracts dispatch times:

#import Dispatch

func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
  {
  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - time1.uptimeNanoseconds )
  }

Compiler says: Ambiguous use of operator ‘-'
Found this candidate
Found this candidate

As usual the candidates are unknown.

What am I doing wrong?

Jan E.


(Saagar Jha) #2

DispatchTimeInterval.nanoseconds expects an Int, while uptimeNanoseconds is an UInt64 (an thus so is their difference). You can “fix” this by adding an explicit cast:

func -(time1: DispatchTime, time2: DispatchTime) -> DispatchTimeInterval {
  return DispatchTimeInterval.nanoseconds(Int(time2.uptimeNanoseconds - time1.uptimeNanoseconds))
}

However, you will need to perform checking for when time2 < time1 and then the difference doesn’t fit in an Int–these will cause crashes.

Saagar Jha

···

On Feb 16, 2017, at 7:56 AM, J.E. Schotsman via swift-users <swift-users@swift.org> wrote:

Hello,

I am trying to define an operator that subtracts dispatch times:

#import Dispatch

func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
  {
  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - time1.uptimeNanoseconds )
  }

Compiler says: Ambiguous use of operator ‘-'
Found this candidate
Found this candidate

As usual the candidates are unknown.

What am I doing wrong?

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


(Jordan Rose) #3

As usual the candidates are unknown.

Just on this particular note, this is a terrible presentation issue and totally something we Apple people need to fix in Xcode, but you can usually see what the candidates were in the build log.

Jordan

···

On Feb 16, 2017, at 07:56, J.E. Schotsman via swift-users <swift-users@swift.org> wrote:

Hello,

I am trying to define an operator that subtracts dispatch times:

#import Dispatch

func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
  {
  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - time1.uptimeNanoseconds )
  }

Compiler says: Ambiguous use of operator ‘-'
Found this candidate
Found this candidate

As usual the candidates are unknown.

What am I doing wrong?

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


(Jon Shier) #4

I’ve had good luck with being able to click the candidates in cases like this so Xcode will navigate to those bits of code. However, in this case I think the issue is that the candidates weren’t from user code but from the standard library, which Xcode has all sorts of issues navigating within, seemingly at random.
  As an aside, it would be really cool if, instead of seeing the standard library as a compiled module, Xcode could navigate to the relevant source code (perhaps optionally). Otherwise I’m stuck either looking at documentation (Apple’s docs or swiftdocs) and if that doesn’t help, trying to navigate to the relevant code in GitHub. This is especially true for things like finding out what the default values defined for functions are, as the documentation just shows default, which is useless.

Jon

···

On Feb 16, 2017, at 1:16 PM, Jordan Rose via swift-users <swift-users@swift.org> wrote:

As usual the candidates are unknown.

Just on this particular note, this is a terrible presentation issue and totally something we Apple people need to fix in Xcode, but you can usually see what the candidates were in the build log.

Jordan

On Feb 16, 2017, at 07:56, J.E. Schotsman via swift-users <swift-users@swift.org> wrote:

Hello,

I am trying to define an operator that subtracts dispatch times:

#import Dispatch

func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
  {
  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - time1.uptimeNanoseconds )
  }

Compiler says: Ambiguous use of operator ‘-'
Found this candidate
Found this candidate

As usual the candidates are unknown.

What am I doing wrong?

Jan E.
_______________________________________________
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


(Rien) #5

I don’t know about the candidates

But the uptimeNanoseconds is a UInt64 and as such you should probably use substractWithOverflow.

var a: UInt64 = 12
var b: UInt64 = 25

let result = UInt64.subtractWithOverflow(a, b)

if result.overflow {
    print("Overflow")
} else {
    let answer = result.0
}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 16 Feb 2017, at 16:56, J.E. Schotsman via swift-users <swift-users@swift.org> wrote:

Hello,

I am trying to define an operator that subtracts dispatch times:

#import Dispatch

func -( time1: DispatchTime, time2: DispatchTime ) -> DispatchTimeInterval
  {
  return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds - time1.uptimeNanoseconds )
  }

Compiler says: Ambiguous use of operator ‘-'
Found this candidate
Found this candidate

As usual the candidates are unknown.

What am I doing wrong?

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


(Zhao Xin) #6

I don't know if you are using an IDE, but in Xcode. I can just cmd+mouse
left click to see the code headers.

Zhaoxin

···

On Fri, Feb 17, 2017 at 2:22 AM, Jon Shier via swift-users < swift-users@swift.org> wrote:

        I’ve had good luck with being able to click the candidates in
cases like this so Xcode will navigate to those bits of code. However, in
this case I think the issue is that the candidates weren’t from user code
but from the standard library, which Xcode has all sorts of issues
navigating within, seemingly at random.
        As an aside, it would be really cool if, instead of seeing the
standard library as a compiled module, Xcode could navigate to the relevant
source code (perhaps optionally). Otherwise I’m stuck either looking at
documentation (Apple’s docs or swiftdocs) and if that doesn’t help, trying
to navigate to the relevant code in GitHub. This is especially true for
things like finding out what the default values defined for functions are,
as the documentation just shows default, which is useless.

Jon

> On Feb 16, 2017, at 1:16 PM, Jordan Rose via swift-users < > swift-users@swift.org> wrote:
>
>> As usual the candidates are unknown.
>
> Just on this particular note, this is a terrible presentation issue and
totally something we Apple people need to fix in Xcode, but you can usually
see what the candidates were in the build log.
>
> Jordan
>
>
>> On Feb 16, 2017, at 07:56, J.E. Schotsman via swift-users < > swift-users@swift.org> wrote:
>>
>> Hello,
>>
>> I am trying to define an operator that subtracts dispatch times:
>>
>> #import Dispatch
>>
>> func -( time1: DispatchTime, time2: DispatchTime ) ->
DispatchTimeInterval
>> {
>> return DispatchTimeInterval.nanoseconds( time2.uptimeNanoseconds
- time1.uptimeNanoseconds )
>> }
>>
>> Compiler says: Ambiguous use of operator ‘-'
>> Found this candidate
>> Found this candidate
>>
>> As usual the candidates are unknown.
>>
>> What am I doing wrong?
>>
>> Jan E.
>> _______________________________________________
>> 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

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


(J.E. Schotsman) #7

Aha! So this was just a confusing compiler message.
Thanks.

Jan E.

···

On 16 Feb 2017, at 18:58, Saagar Jha <saagar@saagarjha.com> wrote:

DispatchTimeInterval.nanoseconds expects an Int, while uptimeNanoseconds is an UInt64 (an thus so is their difference). You can “fix” this by adding an explicit cast: