4 ms·
often times you want to do more stuff in either case so ? does not always work. The if-else approach is more general.
by LinaLauneBaer 12y ago
often times you want to do more stuff in either case so ? does not always work. The if-else approach is more general.
- matt_kantor 12y agoYou could also do this: let x: Something = { if condition { someSideEffect() return foo() } else { anotherSideEffect() return bar() } }() But it's kind of ugly.
- masklinn 12y agoSad that Swift doesn't use an expressive if, then you could just write: let x: Something = if condition { // do stuff foo() } else { // do other stuff bar() }
- 0942v8653 12y agoThat's something I really miss in other languages—the ternary operator is just not good enough for when you have a condition like this. In lisp it's 3 lines which is just the right amount when in other languages you have 6 lines (and until now unnecessary mutability) or only one.