Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Swift does let you declare an immutable variable without assigning a value to it immediately. As long as you assign a value to that variable once and only once on every code path before the variable is read:

    let x: Int
    if cond {
        x = 1
    } else { 
        x = 2
    }

    // read x here


Same with Java and final variables, which should be the default as Carmack said. It’s even a compile time error if you miss an assignment on a path.


that's oldschool swift. the new hotness would be

  let x = if cond { 1 } else { 2 }




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

Search: