[swift-evolution] ternary operator ?: suggestion

Paul Ossenbruggen possen at gmail.com
Sat Dec 12 14:42:13 CST 2015


It is pretty cool your example that works with the existing Swift and I agree with your assessment of it, but good to know. 

I would prefer not to have the brackets in the expressions if possible, Bracket seem a little weird to me inside of an expression. This could also serve to further differentiate a statement vs expression in people’s minds. If the rule is use braces for statements, and no brackets for expressions.  If we needed to use something like a bracket, parenthesis seem more natural for an expression but I think it is better avoid the need for them if possible. Also, not having brackets or parenthesis looks cleaner to me. The commas allow the statement continue without having braces or parenthesis. Keep in mind compounded expressions, having required braces in this example would make it much harder to read:

let v = switch val then .Red: 1, .Green: (if specialGreen then 5 else 2), .Blue: (switch shade then .DarkBlue: 6, .LightBlue: 7, default: 9), default: 4

Use of “then" for the switch expression kind of works if we also want to drop the “case:”. We could use another keyword like “cases” but in general the consensus is to avoid adding keywords unless necessary and “then” with this suggestion, would already be added for “if”. To explore this idea a bit:

let v = switch val cases: .Red: 1, .Green: 2, .Blue: 3, default: 4

this might be more in line with the existing “switch...case” syntax. It seems that this should also work in statements:

switch val {
	cases:	.Red: func1(),
			.Green: func2(),
			.Blue: func3(),
	default: 	func4()
}

This is kind of nice because it reduces the repetition of the word “case:” as well as decreasing visual clutter present in a switch statement. Going back to the expression, it would also be possible to write it like this, if preferred: 

let v = switch val case: .Red: 1, case: .Green: 2, case: .Blue: 3, default: 4

For consistency “cases:” could be allowed here...from your example:

enum Col { cases: Red, Green, Blue }

I suppose as an alternative rather than adding the plural of “case” just use “case:” because it is an existing keyword, as in this example but I think it reads clearer with the plural. 



> On Dec 12, 2015, at 11:15 AM, Al Skipp <al_skipp at fastmail.fm> wrote:
> 
>> On 12 Dec 2015, at 14:48, Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
>> 
>> Dropping “case” might be interesting, since it becomes a little redundant in the “switch” situation, this is one advantage of having a new keyword, but not sure this reads as well:
>> 
>> let v = switch val then .Red: 1, .Green: 2, .Blue: 3
>> 
>> It is definitely nice in it’s compactness which is a big plus. 
>> 
>> Another possibility, because “switch" does not need to resolve the syntactic ambiguity, but then we lose the “then” always meaning an expression consistency. 
>> 
>> let v = switch val case .Red: 1, case .Green: 2, case .Blue: 3
>> 
>> this might be better for switch because we don’t need to mix “then” with “switch” which historically has not been done. Question is, is it better to go with “then” as expression consistency or with slightly more compact and following the conventions out there. Personally, I am not bothered by using “then” with “switch” 
> 
> Would the cases need to be comma separated? Would a new line make more sense instead?
> 
> Currently this is possible:
> 
> enum Col { case Red, Green, Blue }
> 
> let c = Col.Blue
> 
> let x: Int = {
>   switch c {
>   case .Red: return 1
>   case .Green: return 2
>   case .Blue: return 3
>   }
> }()
> 
> It works, but there are several things I don’t like:
> - the switch statement must be wrapped in a closure which is invoked at the end.
> - the type of ‘x’ needs to be given
> - each case requires a return statement 
> 
> Here’s a proposal for a switch expression:
> 
> let y = switch c then {
>   .Red: 1
>   .Green: 2
>   .Blue: 3
> }
> 
> I think the braces are probably needed in the switch expression, also the ‘then’ keyword’ does look a bit peculiar. Any other ideas on how to support both switch statements and expressions?
> 
> Al
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151212/5f6062b7/attachment.html>


More information about the swift-evolution mailing list