英文:
Difference between if as an expression and if as a statement
问题
我正在为您进行翻译,以下是翻译的内容:
所以我在观看关于Go语言的视频 - https://www.youtube.com/watch?v=p9VUCp98ay4 ,大约在6:50的时候,一个人问了一个关于为什么他们将if语句实现为语句而不是表达式的问题。这两种实现有什么区别?据我所知,我从来没有因为语言的不同而改变过使用条件语句的方式。
编辑:他在问题中提到的“你需要值而不是变量”是什么意思?
英文:
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?
答案1
得分: 19
表达式和语句的区别在于,表达式会产生一个值,因此可以在需要值的地方使用。因此,表达式可以作为变量的值、函数的参数或运算符的操作数使用。而语句则不能。
他在问题中提到的“你需要值而不是变量”是什么意思?
我猜他指的是常量(在Scala中称为val
)。
如果if
是一个表达式,你可以这样做:
const myValue = if condition { value1 } else { value2 }
由于if
不是一个表达式,你必须这样做:
var myValue
if condition {
myValue = value1
} else {
myValue = value2
}
所以你需要将变量声明为可变的(使用var
而不是const
),这可能是提问者的意思。
英文:
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 val
s 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.
答案2
得分: 0
你可以通过在GO语言中使用立即调用的函数(IIF)结合if语句来实现与if表达式相同的代码优雅性。我不是GO程序员(主要使用TypeScript),所以如果出于任何原因这对性能不好,请告诉我。
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)
}
然后在程序的后面,而不是有一堆上下文不明的状态,你可以有一个简化的"choice"来选择你的应用逻辑。
if(choice == "taco-bell-time"){
println("Order a bean burrito, but with black beans,")
}
比起
if(amIHungry && didMyPaycheckComeInYet){
println("Order a bean burrito, but with black beans,")
}
更容易理解。
英文:
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,")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论