[swift-evolution] Rename "guard" to "unless"
Loïc Lecrenier
loiclecrenier at icloud.com
Wed Jan 6 04:53:31 CST 2016
Hi James,
“guard" is not the opposite of “if”.
From the Swift book: “You use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed.”
So, unlike “unless”, a “guard" statement must provide an “early exit” in its else clause (fatalError, return, continue, break, …).
IMO, using “unless” would be less expressive, and encourage more misunderstanding of this feature.
Loïc
> On Jan 6, 2016, at 11:09 AM, James Campbell via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list