Hi Everyone,
Back in the ternary discussion, I kinda suggested the use of Guard but then I
realised it’s not a replacement for ternary nor an alternative, it should not
be, but may be a helpful addition to Swift.
let a | x > 0 && x < 10 = “less than ten”
| x > 10 && < 100 = “less than a hundred”
| x > 100 = “more than a hundred”
| otherwise = “probably a negative”
There’s someone who said that it’s reasonable in declarations and another said
that he like to explore using the bar (or pipe) character (|).
The where clause should work well with declarations. For instance, consider a
function which computes the number of solutions for a quadratic equation, ax^2
+ bx + c = 0
let num | disc > 0 = 2
| disc == 0 = 1
| otherwise = 0
where
disc = a2 + b + c
where
a = 2 * 1
b = 2 + 3
c = 6 / 2
I believe it may also help lessen the number of lines (in many cases) from
using if-else-if and/or switch statements.
Anyway, I just thought to start a pitch. If people will like the idea we might
give a proper proposal for the boolean guard.
- Angelo
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution