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

James Campbell james at supmenow.com
Wed Jan 6 09:05:00 CST 2016


Should we add this to the list of things requested that will be rejected ?

On Wed, Jan 6, 2016 at 10:58 AM, Greg Parker <gparker at apple.com> wrote:

>
> > On Jan 6, 2016, at 2: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
>
> `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     Runtime Wrangler
>
>
>


-- 
 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/a2555c14/attachment.html>


More information about the swift-evolution mailing list