[proposal] extra if syntax


(MobileSoft (Piotr)) #1

I think that should be added new ‘postfix’ if syntax:
a = 5 if b == x

the code will be better readable. For example:

a = 5 if b == x
c = 6
d = 8
e = 10 if y == true
f = 12

in this situation all assigns are on the same, left side.

regards
piotr


(Charles Srstka) #2

What’s wrong with:

a = (b == x ? 5 : whateverElseBShouldBe)
c = 6
d = 8
e = (y ? 10 : whateverElseEShouldBe)
f = 12

Charles

···

On May 10, 2016, at 1:10 PM, MobileSoft (Piotr) via swift-evolution <swift-evolution@swift.org> wrote:

I think that should be added new ‘postfix’ if syntax:
a = 5 if b == x

the code will be better readable. For example:

a = 5 if b == x
c = 6
d = 8
e = 10 if y == true
f = 12

in this situation all assigns are on the same, left side.


(Leonardo Pessoa) #3

Despite being interesting and existing in other languages, I believe in Swift this could lead to weird declarations like

let x = 5 if b == 2

Would that mean the variable doesn't get declared if the condition fails? If it gets declared, which will be its value? The types of statements that would allow this construction would have to be limited. Moreover, although this would allow for very readable semantics, the condition is not immediately seen and could lead to misunderstanding of the code.

I believe the current if and ?: syntaxes solve this issue and other needs using a very clean and readable syntax and don't really see a strong point in adding this new construct to the language.

···

On 10 May 2016, at 3:10 pm, MobileSoft (Piotr) via swift-evolution <swift-evolution@swift.org> wrote:

I think that should be added new ‘postfix’ if syntax:
a = 5 if b == x

the code will be better readable. For example:

a = 5 if b == x
c = 6
d = 8
e = 10 if y == true
f = 12

in this situation all assigns are on the same, left side.

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


(Ross O'Brien) #4

In your example:
a = 5 if b == x

What value is assigned to 'a' if 'b' and 'x' are not equal?

···

On Tue, May 10, 2016 at 7:10 PM, MobileSoft (Piotr) via swift-evolution < swift-evolution@swift.org> wrote:

I think that should be added new ‘postfix’ if syntax:
a = 5 if b == x

the code will be better readable. For example:

a = 5 if b == x
c = 6
d = 8
e = 10 if y == true
f = 12

in this situation all assigns are on the same, left side.

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


(Austin Zheng) #5

This is vaguely similar to "if/else and switch as expressions" in the
Commonly Proposed list (
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md),
and probably needs far more justification. (I would argue that it's worse,
since it's less expressive and, as a statement, cannot be composed.)

Austin

···

On Tue, May 10, 2016 at 4:25 PM, Ross O'Brien via swift-evolution < swift-evolution@swift.org> wrote:

In your example:
a = 5 if b == x

What value is assigned to 'a' if 'b' and 'x' are not equal?

On Tue, May 10, 2016 at 7:10 PM, MobileSoft (Piotr) via swift-evolution < > swift-evolution@swift.org> wrote:

I think that should be added new ‘postfix’ if syntax:
a = 5 if b == x

the code will be better readable. For example:

a = 5 if b == x
c = 6
d = 8
e = 10 if y == true
f = 12

in this situation all assigns are on the same, left side.

regards
piotr
_______________________________________________
swift-evolution mailing list
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


(MobileSoft (Piotr)) #6

In your example:
a = 5 if b == x

What value is assigned to 'a' if 'b' and 'x' are not equal?

And what is with ‘a’ if this situation:

if b == x {
  a = 5
}

Nothing :slight_smile:


(Krzysztof Siejkowski) #7

And what is with ‘a’ if this situation:

if b == x {
a = 5
}

Nothing :slight_smile:
Well, it’s not nothing, it has the same value that it had before.

However, as pointed by Leonardo Pessoa, there is a problem with expression:

let a = 5 if b == x

It is equivalent to:

if b == x {

let a = 5 

}

but doesn’t communicate clearly that in the next line `a` is already out of scope and that you cannot write:

let a = 5 if b == x

let c = a // a is not defined here

but you can write:

let a = 1 if b == x

let a = 2 if b == x // other a from other scope

let a = 3 if b == x // another a from another scope

without error concerning immutability.

Austin Zheng has also rightly pointed that this “postfix” syntax looks as if the if-statement is now an expression, which will further confuse people coming from languages that use if-expressions.

Also, I find:

{

// … a lot of code 

} if b == x

way less readable, simply because I read top to bottom. This syntax makes me search for the scope delimiter, understand the condition and only after lets me read the logic.

All the best,

Krzysztof

···

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