[swift-users] ambiguous minus operator

Saagar Jha saagar at saagarjha.com
Thu Feb 16 11:58:09 CST 2017


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 at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170216/604fcca6/attachment.html>


More information about the swift-users mailing list