[Idea] return if / return unless


(Julian Dunskus) #1

How often have you written something like the following?

`
if indexPath.row == 0 { return }

guard let thing = things[index] else { return nil }
`

I propose adding some syntactic sugar to make such statements more readable and simple to write:

`
return if indexPath.row == 0

return nil unless let thing = things[index]
`

I don’t know how easy or hard this would be to implement, but I think it would improve some code significantly.

–Julian Dunskus


(Brent Royal-Gordon) #2

On the `unless` part, at least, see the "Rename guard to unless" section in Commonly Rejected Changes: <https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md>

···

On Aug 4, 2016, at 6:38 AM, Julian Dunskus via swift-evolution <swift-evolution@swift.org> wrote:

return nil unless let thing = things[index]

--
Brent Royal-Gordon
Architechies


(Christian Kienle) #3

+1

···

2016-08-04 15:38 GMT+02:00 Julian Dunskus via swift-evolution < swift-evolution@swift.org>:

How often have you written something like the following?

`
if indexPath.row == 0 { return }

guard let thing = things[index] else { return nil }
`

I propose adding some syntactic sugar to make such statements more
readable and simple to write:

`
return if indexPath.row == 0

return nil unless let thing = things[index]
`

I don’t know how easy or hard this would be to implement, but I think it
would improve some code significantly.

–Julian Dunskus

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Mit freundlichen Grüßen

*Christian Kienle*
Mobile Developer

*REWE Digital GmbH*
Domstraße 20, 50668 Köln, Büro: Schanzenstr. 6-20, 51063 Köln
Geschäftsführer: Dr. Jean-Jacques Michel van Oosten (Vorsitzender),
Christoph Eltze, Dr. Johannes Steegmann, Dr. Robert Zores
Handelsregister: Amtsgericht Köln (HRB 78670) UST-ID-Nr.: DE 290 605 450

Telefon: +49 151 11441782
E-Mail: christian.kienle@rewe-digital.com
Internet: *www.rewe-digital.com <http://www.rewe-digital.com/>*

Ein Unternehmen der *REWE GROUP*
http://www.rewe-group.com


(Kurt Werle) #4

Why would you do this just for return statements? Why not do postfix
conditionals for all statements (like ruby)?

I've always liked postfix conditionals, but not enough to suggest them.
Mostly I'm curious about limiting the scope to return statements.

···

On Thu, Aug 4, 2016 at 7:31 AM, Christian Kienle via swift-evolution < swift-evolution@swift.org> wrote:

+1

2016-08-04 15:38 GMT+02:00 Julian Dunskus via swift-evolution <
swift-evolution@swift.org>:

How often have you written something like the following?

`
if indexPath.row == 0 { return }

guard let thing = things[index] else { return nil }
`

I propose adding some syntactic sugar to make such statements more
readable and simple to write:

`
return if indexPath.row == 0

return nil unless let thing = things[index]
`

I don’t know how easy or hard this would be to implement, but I think it
would improve some code significantly.

–Julian Dunskus

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Mit freundlichen Grüßen

*Christian Kienle*
Mobile Developer

*REWE Digital GmbH*
Domstraße 20, 50668 Köln, Büro: Schanzenstr. 6-20, 51063 Köln
Geschäftsführer: Dr. Jean-Jacques Michel van Oosten (Vorsitzender),
Christoph Eltze, Dr. Johannes Steegmann, Dr. Robert Zores
Handelsregister: Amtsgericht Köln (HRB 78670) UST-ID-Nr.: DE 290 605 450

Telefon: +49 151 11441782
E-Mail: christian.kienle@rewe-digital.com
Internet: *www.rewe-digital.com <http://www.rewe-digital.com/>*

Ein Unternehmen der *REWE GROUP*
http://www.rewe-group.com

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/


(Dave Abrahams) #5

-1 from me. Postfix conditionals are easily mistaken for unconditional
statements when reading quickly. IMO always having the condition first
improves clarity. Furthermore, postfix conditionals don't solve any
real problem in Swift AFAICT.

···

on Thu Aug 04 2016, Kurt Werle <swift-evolution@swift.org> wrote:

Why would you do this just for return statements? Why not do postfix
conditionals for all statements (like ruby)?

I've always liked postfix conditionals, but not enough to suggest them.

--
-Dave


(David Rönnqvist) #6

One complication with allowing postfix conditionals for all statements is that it often needs to have an else clause as well (like the ternary operator “?:").

For example, what’s the type of “x” below?

let x = 5 if someCriteria()

And is “y” initialized or not?

let y: Int
y = 42 if someCriteria()
doSomething(with: y)

I realize that this idea is more about the feature/behavior than the naming, but renaming “guard” to “unless” is one of the commonly rejected proposals.
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md

- David

···

On 04 Aug 2016, at 16:52, Kurt Werle via swift-evolution <swift-evolution@swift.org> wrote:

Why would you do this just for return statements? Why not do postfix conditionals for all statements (like ruby)?

I've always liked postfix conditionals, but not enough to suggest them. Mostly I'm curious about limiting the scope to return statements.

On Thu, Aug 4, 2016 at 7:31 AM, Christian Kienle via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
+1

2016-08-04 15:38 GMT+02:00 Julian Dunskus via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>>:
How often have you written something like the following?

`
if indexPath.row == 0 { return }

guard let thing = things[index] else { return nil }
`

I propose adding some syntactic sugar to make such statements more readable and simple to write:

`
return if indexPath.row == 0

return nil unless let thing = things[index]
`

I don’t know how easy or hard this would be to implement, but I think it would improve some code significantly.

–Julian Dunskus

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Mit freundlichen Grüßen

Christian Kienle
Mobile Developer

REWE Digital GmbH
Domstraße 20, 50668 Köln, Büro: Schanzenstr. 6-20, 51063 Köln
Geschäftsführer: Dr. Jean-Jacques Michel van Oosten (Vorsitzender), Christoph Eltze, Dr. Johannes Steegmann, Dr. Robert Zores
Handelsregister: Amtsgericht Köln (HRB 78670) UST-ID-Nr.: DE 290 605 450

Telefon: +49 151 11441782
E-Mail: christian.kienle@rewe-digital.com <mailto:christian.kienle@rewe-digital.com>
Internet: www.rewe-digital.com <http://www.rewe-digital.com/>

Ein Unternehmen der REWE GROUP
http://www.rewe-group.com <http://www.rewe-group.com/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org
http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Julian Dunskus) #7

I read that, see paragraph three of the email you replied to (my second in this thread)

···

On 05 Aug 2016, at 01:24, Brent Royal-Gordon <brent@architechies.com> wrote:

On Aug 4, 2016, at 6:38 AM, Julian Dunskus via swift-evolution <swift-evolution@swift.org> wrote:

return nil unless let thing = things[index]

On the `unless` part, at least, see the "Rename guard to unless" section in Commonly Rejected Changes: <https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md>


(Julian Dunskus) #8

Having postfix conditionals for return would be a lot easier to implement than having them everywhere, because you just need to watch for an if or unless after the return. Of course they might come in handy elsewhere, and may even solve the ever-present problem of the unintuitive ternary operator ?:, but that’s a whole different discussion. This proposal is simply about having some syntactic sugar for return statements.

Seeing as this change would be purely additive and adding more keywords to Swift is not really a problem (we can use them as parameter labels etc.), I don’t really see a reason against it.

Using “unless” for this makes sense especially as a postfix. I agree with keeping guard around, but this statement is different from guard in that the code executed if the condition can’t be satisfied is stated before the keyword, which makes it read well.

In case of the guard statement, you’d need another keyword to make “unless” logical, like unless foo != nil inWhichCase {}, which just feels bulky to me.

Additionally, return nil guard let thing = things[index] does not read logically to me.

–Julian

···

On 04 Aug 2016, at 17:03, David Rönnqvist via swift-evolution <swift-evolution@swift.org> wrote:

One complication with allowing postfix conditionals for all statements is that it often needs to have an else clause as well (like the ternary operator “?:").

For example, what’s the type of “x” below?

let x = 5 if someCriteria()

And is “y” initialized or not?

let y: Int
y = 42 if someCriteria()
doSomething(with: y)

I realize that this idea is more about the feature/behavior than the naming, but renaming “guard” to “unless” is one of the commonly rejected proposals.
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md

- David

On 04 Aug 2016, at 16:52, Kurt Werle via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Why would you do this just for return statements? Why not do postfix conditionals for all statements (like ruby)?

I've always liked postfix conditionals, but not enough to suggest them. Mostly I'm curious about limiting the scope to return statements.

On Thu, Aug 4, 2016 at 7:31 AM, Christian Kienle via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
+1

2016-08-04 15:38 GMT+02:00 Julian Dunskus via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>>:
How often have you written something like the following?

`
if indexPath.row == 0 { return }

guard let thing = things[index] else { return nil }
`

I propose adding some syntactic sugar to make such statements more readable and simple to write:

`
return if indexPath.row == 0

return nil unless let thing = things[index]
`

I don’t know how easy or hard this would be to implement, but I think it would improve some code significantly.

–Julian Dunskus

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Mit freundlichen Grüßen

Christian Kienle
Mobile Developer

REWE Digital GmbH
Domstraße 20, 50668 Köln, Büro: Schanzenstr. 6-20, 51063 Köln
Geschäftsführer: Dr. Jean-Jacques Michel van Oosten (Vorsitzender), Christoph Eltze, Dr. Johannes Steegmann, Dr. Robert Zores
Handelsregister: Amtsgericht Köln (HRB 78670) UST-ID-Nr.: DE 290 605 450

Telefon: +49 151 11441782
E-Mail: christian.kienle@rewe-digital.com <mailto:christian.kienle@rewe-digital.com>
Internet: www.rewe-digital.com <http://www.rewe-digital.com/>

Ein Unternehmen der REWE GROUP
http://www.rewe-group.com <http://www.rewe-group.com/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

--
kurt@CircleW.org <mailto:kurt@circlew.org>
http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Julian Dunskus) #9

I don’t agree with that. To me, everything between return and if (including the return itself) only being executed if the condition is true seems like the logical way of interpreting that syntax.

I do, however, see how people skimming through code might be confused, but a return with code after it should make it obvious that there’s more to it than always returning at that point.

···

On 05 Aug 2016, at 04:58, rintaro ishizaki <fs.output@gmail.com> wrote:

Limiting for `return` introduces another problem.

mutating func increment() -> Val {
  self = self.incremented()
  return self
}

return val.increment() if val == sentinel
print(val)

I think, it's not obvious `val` after `return ... if` is incremented or not.

2016-08-05 9:11 GMT+09:00 Julian Dunskus via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>>:
On 05 Aug 2016, at 01:24, Brent Royal-Gordon <brent@architechies.com <mailto:brent@architechies.com>> wrote:
>
>> On Aug 4, 2016, at 6:38 AM, Julian Dunskus via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>>
>> return nil unless let thing = things[index]
>
> On the `unless` part, at least, see the "Rename guard to unless" section in Commonly Rejected Changes: <https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md>
>

I read that, see paragraph three of the email you replied to (my second in this thread)
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution