[swift-evolution] [swift-users] Implementing signum

Adrian Zubarev adrian.zubarev at devandartist.com
Sun Nov 20 01:59:38 CST 2016


>From some older evolution-thread: 

extension SignedNumberType { 
 var sign: Self { 
 if self == (0 as Self) { 
 return (0 as Self) 
 } else if self > (0 as Self) { 
 return (1 as Self) 
 } 
 return (-1 as Self) 
 } 
} 
-- 
Adrian Zubarev
Sent with Airmail 

Am 20. November 2016 um 03:44:04, Rick Mann via swift-users (swift-users at swift.org(mailto:swift-users at swift.org)) schrieb:

> 
> 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
> 
> 
> _______________________________________________
> 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-evolution/attachments/20161120/ea891215/attachment.html>


More information about the swift-evolution mailing list