[swift-evolution] ternary operator ?: suggestion

davesweeris at mac.com davesweeris at mac.com
Thu Jan 7 21:07:21 CST 2016


I think it’d be easier to just say:
let x = switch(someValue) {
	case <pattern1>: result1
	case <pattern2>: result2
	case <pattern3>: result3
	default: result4
}

But regardless of the keyword, for the implementation, it could just be wrapped in an implicitly-defined nested function that takes and returns the relevant data types:
func magicFunc1(x: SomeType) -> SomeOtherInferredType {
	switch x {
		case <pattern1>: return result1
		…
	}
}

let x = magicFunc1(someValue)

At least until someone has a better idea.

> On Jan 6, 2016, at 23:34, John Randolph via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Jan 6, 2016, at 11:23 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Something akin to:
>> 
>> let x = someValue ? 
>>  case <pattern1>: result1
>>  case <pattern2>: result2
>>  case <pattern3>: result3
>>  default: result4
> 
> This kind of feels like “switch with a return value”, which might be a useful new construct, but I would advocate that if implemented, it gets a new keyword.  Maybe like…
> 
> 	let x = pick(someValue){
> 	  	case <pattern1>: result1
> 	 	case <pattern2>: result2
> 	 	case <pattern3>: result3
> 		default: result4
> 		}
> 
> but in practice, I’d be hard pressed to choose between this or something like making an array or dictionary of closures.
> 
> -jcr
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list