[swift-users] pattern matching in if improvement?

Neil Faiman neil.swift at faiman.org
Sat Mar 26 18:09:55 CDT 2016


> On Mar 26, 2016, at 5:47 PM, Maury Markowitz via swift-users <swift-users at swift.org> wrote:
> 
> 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
> 

How about

	if (200..<299).contains(statusCode)



More information about the swift-users mailing list