[swift-evolution] Rename "guard" to "unless"

James Campbell james at supmenow.com
Wed Jan 6 04:09:44 CST 2016


What is "guard"? why its the opposite to "if"!

So in other languages what have they called this, well in Ruby they called
it "unless" which I feel is a much clearer form of intent and lower barrier
of entry for a user. See this code.

guard name != nil else {
   fatalError("Please enter a name")
}

What does that actually say if you look at it from a linguistic point of
view, "guard that name doesn't equal nil otherwise fail", that feels very
obtuse.

With my proposal the syntax could become this:

unless name != nil {
  fatalError("Please enter a name")
}

This now reads as "unless name doesn't equal nil then fail" which I think
is a much clearer syntax. I think this would be a great change for Swift 3.
For me I think this is more friendly for beginners.

It would support the same structure as an if block:

unless name != nil {
}
else
{
}

Going forward it would allow us to be more flexible if we added inline
if/unless statement, as in Ruby.

callThisMethod() if age > 0
throwThisError() unless age <= 0

-- 
 Wizard
james at supmenow.com
+44 7523 279 698
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/6a1d41a1/attachment.html>


More information about the swift-evolution mailing list