[swift-evolution] ternary operator ?: suggestion

Daniel Valls Estella daniel at upzzle.com
Sat Dec 12 19:45:43 CST 2015


I don’t see the current way far 

var optSel = "opt2"

let optRes:Int = { switch str{
		
	case "opt1": return 1
	case "opt2": return 2
	default: return 0
		
	} }()


To beauty this maybe operators can come in downgrading eficiency:

let optSel = "opt2"

let optRes = optSel *= [
	
	("opt1", 1),
	("opt2", 2)
	
] ?? 3

With (pseudocode):

func *= (left: Any, right: [(Any,Any)]) -> Any? {

	for (rightKey,rightValue) in right{
		
		if rightKey == left {
				
			return rightValue
		}
	}
	
	return nil
}

Or:

let optSel = "opt2"

let optRes = optSel *= [
	
	"opt1": 1,
	"opt2": 2
	
	] ?? 3


With (just conceptually):

func *= (left: Any, right: Dictionary<Any,Any>) -> Any? {
	
	for rightKey in right.keys(){
		
		if rightKey == left {
			
			return right[rightKey]
		}
	}
	
	return nil
}

> El 13 des 2015, a les 0:51, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> va escriure:
> 
> 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
> 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/f659951a/attachment.html>


More information about the swift-evolution mailing list