Hacker News new | past | comments | ask | show | jobs | submit login

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?



    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"




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: