News for hackers. Stuff that matters.
Re:Swift 1.2 and Xcode 6.3 beta by melling on Monday February 09, 2015 @02:28PM (#9023111)
This is practical. It's something you wish for within a couple hours of Swift.

let x : SomeThing

if condition {

   x = foo()
} else {

   x = bar()
}

use(x)

Re:Swift 1.2 and Xcode 6.3 beta by spott on Monday February 09, 2015 @04:08PM (#9023745)
so, in c++ you can do initialization of const value via lambda closures that are immediately evaluated:

    const int a = [=](){ 
        //do stuff here that returns an int:
        if (condition) { return foo(); } else { return bar(); }
    }();
Does this idiom work in swift?
Re:Swift 1.2 and Xcode 6.3 beta by lyinsteve on Monday February 09, 2015 @04:14PM (#9023795)

    let x: String = {
       if 3 == 4 {
          return "Hello"
       } else {
          return "Goodbye"
       }
    }()
Although in this case, I'd just use ternary.

    let x = (3 == 4) ? "Hello" : "Goodbye"
Re:Swift 1.2 and Xcode 6.3 beta by bigdubs on Monday February 09, 2015 @02:32PM (#9023141)
short of having a language mechanism for doing an 'uninitialized' let, conditional operators could work:

let x : Something = condition ? foo() : bar()

but yeah, it would be nice to defer initialization of an immutable.

Re:Swift 1.2 and Xcode 6.3 beta by LinaLauneBaer on Monday February 09, 2015 @02:35PM (#9023155)
often times you want to do more stuff in either case so ? does not always work. The if-else approach is more general.
Re:Swift 1.2 and Xcode 6.3 beta by matt_kantor on Monday February 09, 2015 @03:07PM (#9023332)
You could also do this:

    let x: Something = {
      if condition {
        someSideEffect()
        return foo()
      } else {
        anotherSideEffect()
        return bar()
      }
    }()
But it's kind of ugly.
Re:Swift 1.2 and Xcode 6.3 beta by masklinn on Monday February 09, 2015 @03:31PM (#9023489)
Sad 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()
    }
Re:Swift 1.2 and Xcode 6.3 beta by 0942v8653 on Monday February 09, 2015 @10:03PM (#9025155)
That'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.
Re:Swift 1.2 and Xcode 6.3 beta by mikeash on Monday February 09, 2015 @02:40PM (#9023180)
Note that it's no longer "would," this is a feature included in this beta. OP's code is now legal.
Re:Swift 1.2 and Xcode 6.3 beta by jafingi on Monday February 09, 2015 @02:34PM (#9023151)
Let's hope you don't have a baz.
Re:Swift 1.2 and Xcode 6.3 beta by bigdubs on Monday February 09, 2015 @03:06PM (#9023331)
you can torture the logic further:

let x : Something = outer_conditional ? baz() : conditional ? foo() : bar()

Re:Swift 1.2 and Xcode 6.3 beta by mikeash on Monday February 09, 2015 @03:12PM (#9023371)
I think this can look pretty nice, properly formatted:

    let x = conditional1 ? bar() :
            conditional2 ? baz() :
            conditional3 ? quux() :
            someDefault
Re:Swift 1.2 and Xcode 6.3 beta by netcraft on Monday February 09, 2015 @02:49PM (#9023224)
I wish the es6 const would allow this.