[swift-users] pattern matching in if improvement?

Wallacy wallacyf at gmail.com
Sat Mar 26 17:19:45 CDT 2016


var someInteger = 250
if 200..<299 ~= someInteger {
  print("works")
}

Em sáb, 26 de mar de 2016 18:47, Maury Markowitz via swift-users <
swift-users at swift.org> escreveu:

> Before I stick my head into the other list, consider:
>
>    if statusCode >= 200 && statusCode <= 299
>
> I'm sure examples of something like this occur throughout your code. But
> the actual semantics of the test is hidden here, you're really testing if
> statusCode lies within a range. Swift 2.0 has a couple of options for this,
> but I find them all rather odd. The most recommended is:
>
>    if case 0...100 = someInteger
>
> This syntax has problems. For one thing, it's written backwards compared
> to most people's code...
>
>    if someinteger == 100
>
> not...
>
>    if 100 == someinteger
>
> so it just *feels* wrong. In addition, the use of "case" seems odd too.
> And finally, there's the use of the single equals sign in a test, which
> goes against everything we've learned in C-like languages.
>
> So unless I'm missing something, can anyone offer a reason this wouldn't
> work?
>
>   if someinteger in 0...100
>
>
> _______________________________________________
> 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/20160326/620c1fd9/attachment.html>


More information about the swift-users mailing list