[swift-evolution] ternary operator ?: suggestion
Jo Albright
me at jo2.co
Wed Jan 6 11:03:09 CST 2016
Just a couple of thoughts swirling in my brain.
1. Ternary means : composed of three parts …. if we add case functionality, it no longer is a ternary and should be renamed (ex : nil coalescing … was used instead of calling it a ternary optional). Would love to hear thoughts on what names it could have … if the proposal moves forward.
---
I am leaning towards this pattern (someone else introduced), yet I don’t feel it is perfect.
let val = color ? .Red: 0xFF0000, .Green: 0x00FF00, .Blue: 0x0000FF, _: 0xFFFFFF
2. I have a problem with “_”. I don’t agree with it being the default value. It is currently used to discard or ignore values … using it as a default will add unnecessary confusion that does not align with Swift. Writing out the word default instead of _ is only 6 more characters. Please correct me if I am wrong on this note.
let val = color ? .Red: 0xFF0000, .Green: 0x00FF00, .Blue: 0x0000FF, default: 0xFFFFFF
3. Having “?” for the Bool operator and “:” for the cases presents confusion with the current ? : ternary design pattern. This is another potential issue … just not a big of a deal as the “default as _”.
let val = color case .Red: 0xFF0000, .Green: 0x00FF00, .Blue: 0x0000FF, default: 0xFFFFFF
let val = color ??? .Red: 0xFF0000, .Green: 0x00FF00, .Blue: 0x0000FF, default: 0xFFFFFF
And some less serious ones.
switch let val = color case .Red: 0xFF0000, .Green: 0x00FF00, .Blue: 0x0000FF, default: 0xFFFFFF
case let val = color ??? .Red: 0xFF0000, .Green: 0x00FF00, .Blue: 0x0000FF, default: 0xFFFFFF
I do believe this functionality is worth adding to Swift core lib. I just want to see it as closely aligned with Swift as possible.
Nerd . Designer . Developer
Jo Albright
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/8872e336/attachment.html>
More information about the swift-evolution
mailing list