[swift-evolution] Rename "guard" to "unless"
Chris Lattner
clattner at apple.com
Wed Jan 6 12:34:13 CST 2016
On Jan 6, 2016, at 7:05 AM, James Campbell via swift-evolution <swift-evolution at swift.org> wrote:
> Should we add this to the list of things requested that will be rejected ?
Yes, if you send a good PR adding it, I’ll merge it. Note that it was (very briefly) named “unless” as a placeholder until May 1, 2015. We carefully bikeshedded this and settled on guard.
-Chris
>
> On Wed, Jan 6, 2016 at 10:58 AM, Greg Parker <gparker at apple.com <mailto:gparker at apple.com>> wrote:
>
> > On Jan 6, 2016, at 2:09 AM, James Campbell via swift-evolution <swift-evolution at swift.org <mailto: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
>
> `guard` is not the opposite of `if`. `guard` has effects that `if` does not. We deliberately chose not to provide syntax that was identical to `if not X`.
>
> History: The very first proposal of this feature was called `unless`. Fifty-odd messages of discussion later it was called `require`, in part because we did not want to match the `if not` constructs seen in languages like Perl and Ruby. That name was still controversial. A few weeks later it was changed to `guard..else` to general acclaim because the intended use is commonly known as a "guard clause".
>
>
> --
> Greg Parker gparker at apple.com <mailto:gparker at apple.com> Runtime Wrangler
>
>
>
>
>
> --
> Wizard
> james at supmenow.com <mailto:james at supmenow.com>
> +44 7523 279 698
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/e010920f/attachment.html>
More information about the swift-evolution
mailing list