[swift-evolution] ternary operator ?: suggestion

Craig Cruden ccruden at novafore.com
Sun Jan 10 03:52:10 CST 2016


You should be able to write code that runs (on imaginary machines) the first time :o

I guess the first one can be considered syntactic sugar for the second one for a specific case so to speak.   Both of them being really just partial functions which combine to make a full function and can be used wherever a closure/block of code can be used.  (i.e. reduce, filter, etc. on collections).

-------

OT: Unfortunately it does not happen as much these days as I would like …. but at least I have my memories of writing a 3 or 4 page IBM mainframe (forget which one)…. assembly language program that ran the first time…. so long ago…. but then there was an old 8086 assembly language program (think it was close to 100 pages printed) that I never did figure out why it was losing a character (communications) every once in a while — that one still haunts me :o…. [luckily it was just playing around]


> On 2016-01-10, at 13:09:56, Paul Ossenbruggen <possen at gmail.com> wrote:
> 
> Yep good point, should have double checked it. Where is my compiler? :-). Here is the corrected version:
> 
> 	let num = color.map {
> 		cases   .Red: 100, 
> 			     .Green:  200, 
> 			     .Blue: 300
> 		default: -1 
> 	}
> 	let num = color.map {
> 		case     .Red: 100
> 		case     .Green:  200 
> 		case     .Blue: 300
> 		default: -1 
> 	}
> 
>> On Jan 9, 2016, at 10:00 PM, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>> 
>> Also I don’t think it would be “case:” just “case <pattern> where <expression>:” or “cases .Red: …, .Green: …”
>> 
>> 
>>> On 2016-01-10, at 12:56:19, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>>> 
>>> The separation comma would not be applicable to “case: “ since what follows is a block of code (and “,” is not used in code that I know of)….. up until the next “case:”/“default:" or “}" — the last line of the code would evaluate to what is being mapped to.
>>> 
>>> 
>>> 
>>>> On 2016-01-10, at 12:42:14, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>> Sorry meant this to say "Craig and Thorsten”  
>>>> 
>>>>> On Jan 9, 2016, at 9:41 PM, Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
>>>>> 
>>>>> Hi Charles,
>>>>> 
>>>>> Yep, someone needs to build a bike shed otherwise the bike gets all wet, but what color should it be? :-) 
>>>>> 
>>>>> I agree that it would be really useful to keep things concise. I am going to suggest again an idea I had in the past, it is also in my proposal, which might work well for this problem. This might address the verbosity of the “case” and at the same time make it obvious we are dealing with a switch expression. So both would be valid: 
>>>>> 
>>>>> 	let num = color.map {
>>>>> 		cases:   .Red: 100, 
>>>>> 			      .Green:  200, 
>>>>> 			      .Blue: 300,
>>>>> 		default: -1 
>>>>> 	}
>>>>> 
>>>>> 	let num = color.map {
>>>>> 		case:   .Red: 100, 
>>>>> 		case:   .Green:  200, 
>>>>> 		case:   .Blue: 300,
>>>>> 		default: -1 
>>>>> 	}
>>>>> 
>>>>> So this is good in that we can avoid duplicating the word “case” which makes it much more concise. Because expressions don’t need the separating abilities of the word “case" between each group of statements, as there can only be one expression after each label. We list the matching cases,  the if and when we need the default, we list that. This is both familiar and close enough to the existing “case". And it allows you to also use “case” for each one if you prefer. The other advantage is that we can key off first “case or cases”, as Chris mentioned, and it stands out as a switch expression and can not be confused with a dictionary. 
>>>>> 
>>>>> So on the single line version:
>>>>> 
>>>>> 	let num = color.map { cases: .Red: 100,  .Green:  200, .Blue: 300 default: -1 }
>>>>> 
>>>>> It looks pretty decent, it is pretty concise, there is no duplication, everyone should know what it means, the case part and the default part stand out pretty nicely. It would also make a hybrid line setup pretty easy as well:
>>>>> 
>>>>> 	let num = color.map { 
>>>>> 		cases: .Red: 100,  .Green:  200, .Blue: 300,
>>>>> 	        default: -1 
>>>>> 	}
>>>>> 
>>>>> This combined with the case syntax for code blocks from Scala, and map that Charles and Thorsten, have been talking about seems like a pretty decent idea to me.
>>>>> 
>>>>> - Paul
>>>>> 
>>>>>> On Jan 9, 2016, at 5:58 PM, Charles Constant <charles at charlesism.com <mailto:charles at charlesism.com>> wrote:
>>>>>> 
>>>>>> 
>>>>>> 	let num = color.map {
>>>>>> 		.Red: 100, 
>>>>>> 		.Green:  200, 
>>>>>> 		.Blue: 300,
>>>>>> 		_ : -1 
>>>>>> 	}
>>>>>> 
>>>>>> 
>>>>>> Looks like I forgot to mention what my previous message regards: I mean that if we go with "map", I prefer the version that doesn't require "case" be written.
>>>>>> 
>>>>>> I also realize whether we're verbose enough depends on how commonly people use this type of expression. If it is accepted, but unpopular, then I'd concede "verbose" makes more sense. 
>>>>>> 
>>>>>> Anyways, I'll try to lurk for a while now, instead of posting, as I'm veering toward the bike-shed :)
>>>>>> 
>>>>>> 
>>>>> 
>>>> 
>>>>  _______________________________________________
>>>> 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/20160110/938a0c72/attachment.html>


More information about the swift-evolution mailing list