Skip to content

Instantly share code, notes, and snippets.

@stoeckley
Created December 31, 2017 23:58
Show Gist options
  • Save stoeckley/f6da4a656b911d247e827ef2a15badc4 to your computer and use it in GitHub Desktop.
Save stoeckley/f6da4a656b911d247e827ef2a15badc4 to your computer and use it in GitHub Desktop.
how to initialize a let based on conditional branches
// i often do this just so i can make a variable a constant let and not a var;
// with a var I could just use a switch, but it's better to have a let when possible
// how to make this prettier?
let someConstant: Int64 =
someBoolExpression ? 1 :
someOtherBoolExpression ? 2 :
yetAnotherBoolExpression ? 3 : 0
// or perhaps this style of code is just acceptable?
@stoeckley
Copy link
Author

let x: Int
if condition {
    x = 3
} else {
    x = 5
}

and

let foo = {
    switch (a, b, c) {
    case (true, true, _):
        return 1
     case (false, _, _):
         return 2
    ...
    }
}()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment