And everyone agreed so hard that it was removed from almost every modern C replacement (Rust, Nim, Zig, Elixir, Kotlin).
And everyone agreed so hard that it was removed from almost every modern C replacement (Rust, Nim, Zig, Elixir, Kotlin).
val foo = if (bar) "this" else "that"
and you don't need two separate assignments. foo := func() string {
if bar { return "this" }
return "that"
}() foo := "that"
if bar {
foo = "that"
}
Unless the assignment of "foo" is expensive, then you'd assign it in an else.If you really want to, you can do it in a single line too:
foo := map[bool]string{true: "this", false: "that"}[bar]> and you don't need two separate assignments.
Although I do use it far more often nowadays than a decade ago. I've even mentioned it before https://news.ycombinator.com/item?id=30384835.
They removed it because they made the regular "if" statement into an expression, so the separate "ternary" operator expression is superfluous. Rust et al made the "if" situation better than C; Go made it worse.
Rust, Nim, Zig, and Go are all fair game in that list, as far as I know being a C replacement is in the mission statement for the creation of all those languages.
https://ziglang.org/documentation/master/#if
Also, Zig is so into expressions over statements that they plan to remove “function definition as statement”, which I think is awesome. In other words, conceptually function definition is a compile-time expression that creates the function.
Edit: here’s that Zig rfc:
foo = if x, do: 1, else: 2