[swift-users] Implementing signum

Rick Mann rmann at latencyzero.com
Sat Nov 19 20:44:04 CST 2016


I'm trying to do this:

protocol Signumable
{
	func sgn() -> Self
}

extension Signumable
{
	func
	sgn()
		-> Self
	{
		let pos = 0 < self		//  Error
		let neg = self < 0		//  Error
		let p = pos ? Self(1) : Self(0)
		let n = neg ? Self(1) : Self(0)
		return p - n
	}
}


extension Double : Signumable {}

But I get

Binary operator '<' cannot be applied to operands of type 'Int' and 'Self'

I figure there should be additional constraints on Signumable.

Help would be much appreciated!


-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list