[swift-evolution] ternary operator ?: suggestion
possen at gmail.com
Wed Dec 16 01:08:39 CST 2015
I think pick…from may address many of the negatives listed by Chris. with the if..then..else approach.
> The closest proposal I’ve seen is the “if cond then value1 else value2” syntax, however that has serious (IMO) problems:
> - It is substantially more verbose than ?:, so much so that it obscures the logic that was trying to be captured. Simple things like this become swallowed in syntax:
> let x = cond ? 4 : 8
> let x = if cond then 4 else 8
this suggestion would still be longer, you can’t beat 2 chars. It is shorter than if..then..else though. Not sure there are any good options that can beat the conciseness of the ternary without replacing it with more syntax.
let x = pick cond from 4, 8
> - Because it looks like an if statement, people will end up writing it like:
> let x = if cond then
> - it only accepts expressions, not statements. The way it is flowed makes it look like a statement.
> - It is now force indenting a lot, which just looks weird and isn’t precedented in Swift.
By having a new keyword, we can define the format and there is no prior connotation with it.
> When this happens, we now have new problems:
> - At a glance, it “looks” like an if statement, but it is semantically different.
Since it is a new it will always look like something that handles expressions only.
Also this pick…from statement can be extended to handle switch expressions as well. So it is consistent way to do different expressions based upon an input.
no prior history with other programming languages.
> On Dec 15, 2015, at 4:31 PM, Paul Ossenbruggen <possen at gmail.com> wrote:
> Been thinking a bit:
> Perhaps a new expression is in order. “Pick” this has a form like this. Param is a selector This only allows expressions
> It has two forms:
> To replace ternary:
> let x = pick val from "abc", "cdef"
> To replace switch expressions. The cases follows existing rules for switch cases.
> let y = pick val from cases .Red: 1, .Green: 2, .Blue: 3
> This keeps the notion of expressions and statements quite separate. It avoids syntax confusion. It reads clear. It is fairy concise. It uses a straight forward pattern for both forms of expression.
> - Paul
>> On Dec 15, 2015, at 2:06 PM, Charles Constant via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> +1 bigtime for the assignment via Switch proposal
>> I think someone here made the argument, I can't remember who, that it would be confusing for beginners. I think exactly the opposite.
>> Once a new programmer has learned how to write a normal Switch statement, they'll be able to "leverage" the same concept and map values using the Switch assignment. Some might even try it on their on own, through experimentation, to see if it will work. It's such a pleasant experience when you try something in a language that seems consistent with what you already know, and discover "cool, it works!"
>> At the moment, the alternatives are, what, using a dict to map values? trying to shoehorn a corrsponding set of values into an enum? using the existing switch statement (pretty verbose in Swift, due to "let" scope etc)? In my own Swift code, I have encountered situations, frequently, where I wished I had an equivalent to a ternary condition that handled more than two values. Chaining multiple ternary conditions together is unreadable. This proposed Switch assignment expression would take care of that.
>> Definitely has my vote!
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution