[swift-evolution] ternary operator ?: suggestion

Paul Ossenbruggen possen at gmail.com
Sun Dec 13 12:04:30 CST 2015


I see that this will help with consistency, this would address the question of when to use “then".  I would not want to require it on statements. I like how you can simply put the brace after then condition in the existing Swift. It would be required for expression but not for statements. I can add this change if others like it too.


> On Dec 13, 2015, at 6:39 AM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
> 
> Hi Paul,
> 
> thanks for gathering the ideas in a proposal!
> 
> You suggested to use if…then…else instead of just using the statement form if … {…} else {…}.
> In that case I wonder whether it wouldn’t be better to use „then“ in if-statements as well for consistency:
> 
> if condition then {
>> } else {
>> }
> 
> For switch-expressions I’d just keep the statement syntax as there is only one pair of braces and not one per case. Separating the expressions, for which „then“ had to be introduced in case of the if-expression, is already done nicely by the „case“ keywords. 
> Here, too, my intent would be to keep the syntax of expression and statement the same for consistency.
> 
> Some remarks on the section about alternatives:
> 
>> The problem with this is that in expressions all cases must be handled. In a statement it is common to not have else. So you could make it an expression if the return value is used. This makes it more complex from both the user's perspective and the compiler perspective. That means that if you were to change a "if" conditional into an expression suddenly you would get an an error if the "else" part was not provided.
> 
> 
> I don’t see any problem here. The only rule is that expressions have to have an else clause. This is independent of syntax i.e. whether we uses braces or „then".
> 
> Especially for the switch expression I don’t think the standard statement syntax with braces looks bad (as there is only one pair of braces around all cases and not one per case like in the if-statement), at least if you format it a little bit different than in your proposal and drop the unnecessary parentheses:
> 
> let color =
>     switch enumColor {
>     case .Red:
>         switch shade {
>         case .DarkRed: 0xFFEEEE
>         case .LightRed: 0xFF0000
>         default: 0xFF1010
>         }
>     default: 0xFFFFFF
>     }
> 
> -Thorsten
> 
>> Am 13.12.2015 um 06:54 schrieb Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>> 
>> Hello All, 
>> 
>> Been sick in bed all day, but decided to try to be productive…
>> 
>> I did a rough draft of a proposal for implementing if expressions and switch expressions based upon the discussions we had here. I have tried to keep the scope of the changes as small as possible,  only added one keyword and kept things as similar to the existing language constructs as possible. If anyone wants to help me with this, or has feedback, please let me know,
>> 
>> https://github.com/possen/swift-evolution/blob/master/0020.md <https://github.com/possen/swift-evolution/blob/master/0020.md>
>> 
>> Thanks,
>> - Paul
>> 
>> 
>> 
>>> On Dec 12, 2015, at 3:51 PM, Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
>>> 
>>> Implied in using the  “then", if…then…else would aways require “else" when using “then” similar to how “guard" requires “else”. This  will help to make the difference between statements and expressions clear.
>>> 
>>> let x = If cond then X else Y
>>> 
>>> is the full form, where “else" can not be omitted. 
>>> 
>>>> On Dec 12, 2015, at 12:59 PM, Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
>>>> 
>>>> 
>>>> 
>>>>> On Dec 12, 2015, at 12:37 PM, Andrey Tarantsov via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>> 
>>>>> 1. I would really hate to explain to someone when if needs a then and when it doesn't. That's the sort of inconsistency that shouldn't be added lightly.
>>>> 
>>>> agreed definitely want to be careful with that. I think with braces meaning statements that differentiation can be made clear. I would certainly start with statements when describing, just as you usually don’t talk about the ternary operator until later. 
>>>> 
>>>>> 3. If we can somehow solve all of this, I think I'll be +1 for replacing (A ? B : C) with some sort of (if A then B else C).
>>>> 
>>>> Yes that would be great.
>>>> 
>>>>> 
>>>>> 4. Generally, I wonder how hard would it be for all statements to be usable as expressions? Why didn't Swift go that way from the start?
>>>> 
>>>> The biggest problem statement is you don’t need to exhaustively specify every outcome:
>>>> 
>>>> if cond {
>>>> 	print(“hello”)
>>>> }
>>>> 
>>>> whereas in an expression you have to specify what happens in the else.
>>>> 
>>>> let say = if cond then “hello” else “goodbye"
>>>> 
>>>> unless you go seriously off the deep end:
>>>> 
>>>> let say = if cond then “hello” 
>>>> 
>>>>  “say" then becomes an optional, *shudder*
>>>> 
>>>> 
>>> 
>> 
>>  _______________________________________________
>> 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/20151213/4724c8be/attachment.html>


More information about the swift-evolution mailing list