[swift-evolution] divisible-by operator

Haravikk swift-evolution at haravikk.me
Thu Apr 7 06:20:48 CDT 2016


Personally I’d prefer to use a method like:

	21.isDivisibleBy(3)

As it’s clearer IMO. My problem with the operator is that it doesn’t make logical sense to have the value you’re dividing by on the right hand side when there are equals signs, it seems more like this should be the value you’re testing. Perhaps there’s a better choice of operator such as:

	21 %: 3

Though that’s probably not very clear either as the colon could be easy to miss. Basically the choice of %== doesn’t suggest its purpose, which makes it one more thing to learn, which is why a method may be a better choice.

> On 6 Apr 2016, at 17:13, Milos Rankovic via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Checking for divisibility is very common:
> 
>     21 % 3 == 0 // true
> 
> In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:
> 
>     21 %== 3 // true
> 
> … which also allows me to overload it for sequences:
> 
>     21 %== [7, 3] // true
> 
> (If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)
> 
> milos
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160407/b6758e1e/attachment.html>


More information about the swift-evolution mailing list