Difference between if as an expression and if as a statement
Asked Answered
J

2

9

So I was watching this video on the Go language - https://www.youtube.com/watch?v=p9VUCp98ay4 , and at around 6:50 a guy asks a question about why they implemented if's as statements and not expressions. What is the difference between those two implementations? As far as I know, I've never had to change the way I use a conditional based on the language.

Edit: and what does he mean that "you need values rather than variables" in his question?

Joselow answered 27/6, 2017 at 1:17 Comment(3)
Haven't watched in order to be able to answer the second question you asked, but to the first question, if as-an-expression would be where x = if (condition) then { value1 } else { value2 } can assign different values to x. Rust and Kotlin do it, for example, and it's what C's ternary operator (?:) does (same example is written condition ? value1 : value2).Beaulahbeaulieu
It's a language design decision despite the asker's use of the word 'implementation'. If I had to guess why the Gophers designed it that way, it's that Go generally sticks to the imperative tradition of C, and blocks under if behaving as statements rather than like expressions is more C-like.Beaulahbeaulieu
You might want to take a look at the BLISS programming language. You can find documentation on line. Everything is an expression in BLISS.Andromada
A
22

The difference between expressions and statements is that expressions produce a value and thus can be used in places where values are required. So expressions can be used as values for variables, arguments to functions or operands to operators. Statements can't.

and what does he mean that "you need values rather than variables" in his question?

I assume that by vals he means constants (which are called vals in Scala for example).

If if were an expression, you could do this:

const myValue = if condition { value1 } else { value2 }

Since if is not an expression, you have to do this:

var myValue
if condition {
    myValue = value1
} else {
    myValue = value2
}

So you needed to make your variable mutable (use var instead of const), which is what the person asking the question likely meant.

Anguilliform answered 27/6, 2017 at 1:29 Comment(0)
L
0

You can get the same code elegance that you would get from if expression by using an instantly invoked function (IIF) combined with if statements in GO. I'm not a GO programmer (mainly typescript) so please let me know if this is bad for performance for any reason.

func main() {
    amIHungry := true
    didMyPaycheckComeInYet := false

    choice := func() string {
        if(!amIHungry && !didMyPaycheckComeInYet){
            return "stay-home"
        }
        if(!amIHungry && didMyPaycheckComeInYet){
            return "buy-new-keyboard"
        }
        if(amIHungry && !didMyPaycheckComeInYet){
            return "make-ramen"
        }
        return "taco-bell-time"
    }()

    println(choice)

}

and then later on in your program, rather than having a bunch of out of context states, you can have a simplified "choice" to choose your app logic.

if(choice == "taco-bell-time"){
   println("Order a bean burrito, but with black beans,")
}

is a little easier to reason about than

if(amIHungry && didMyPaycheckComeInYet){
     println("Order a bean burrito, but with black beans,")
}
Labrie answered 11/1, 2022 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.