Could if
statement makes sense here?
#2158
Replies: 10 comments 32 replies
-
Personally I'd like an if/else that:
if foo() && bar() && quux() {
True
} else {
False
} ...goes into That being said, except for reserving if and else as keywords this can be added quite easily down the line, and the gains are small I guess, so IMHO this is quite low prio. |
Beta Was this translation helpful? Give feedback.
-
I think |
Beta Was this translation helpful? Give feedback.
-
I think having if/else helps with strangeness budget. We could even support if/elseif/else by throwing a specific compiler error that says to use case? |
Beta Was this translation helpful? Give feedback.
-
What about adding a multi-way conditional expression, like Currently you'd write: case c1 {
True -> e1
False -> e0
} (38 characters). if c1 {
e1
} else {
e0
} (28 characters). when {
c1 -> e1
else -> e0
} (32 characters) would be readable and easily expandable. Take an example with more boolean tests: case c1 {
True -> e1
False -> case c2 {
True -> e2
False -> e0
}
} (80 characters). if c1 {
e1
} else { if c2 {
e2
} else {
e0
} } (52 characters when formatted without an extra indent). when {
c1 -> e1
c2 -> e2
else -> e0
} (42 characters). This would:
Another benefit: using this multi-way if, the right-right-hand side of the arrows can be single expressions in most cases, and block expressions only when needed, like in Gleam's |
Beta Was this translation helpful? Give feedback.
-
Isn't this currently possible and still more concise than case any_irrelevant_value {
_ if x < 10 -> io.println("small")
_ if x < 100 -> io.println("medium")
_ -> io.println("large")
} we could allow this at least: case _ {
_ if x < 10 -> io.println("small")
_ if x < 100 -> io.println("medium")
_ -> io.println("large")
} Allowing the use of the underscore (as shown above) in place of needing to supply an irrelevant value... it's a consistent syntax. |
Beta Was this translation helpful? Give feedback.
-
Dropping more syntax: |
Beta Was this translation helpful? Give feedback.
-
case {
if x < 10 -> io.println("small")
if x < 100 -> io.println("medium")
_ -> io.println("large")
} |
Beta Was this translation helpful? Give feedback.
-
case {
x < 10 -> io.println("small")
x < 100 -> io.println("medium")
_ -> io.println("large")
} This would be roughly equivalent to kotlin's // Kotlin
// "when" can be used as an alternative to "if-else if" chains.
val i = 10
when {
i < 7 -> println("first block")
fooString.startsWith("hello") -> println("second block")
else -> println("else block")
}
// "when" can be used with an argument.
when (i) {
0, 21 -> println("0 or 21")
in 1..20 -> println("in the range 1 to 20")
else -> println("none of the above")
} I think these possibilities are cleaner than |
Beta Was this translation helpful? Give feedback.
-
This is currently valid syntax: case x < 10 {
True -> "small"
False ->
case x < 100 {
True -> "medium"
False ->
case x < 1000 {
True -> "large"
False -> "extra large"
}}} It reads nicely. |
Beta Was this translation helpful? Give feedback.
-
I love Gleam dedication for minimalism to only have one type of flow control: import gleam/option.{type Option, None, Some}
import gleam/io
fn if_then(requirement: Bool, consequence: fn() -> a) -> Option(a) {
case requirement {
True -> Some(consequence())
False -> None
}
}
fn elif_then(value: Option(a), requirement: Bool, consequence: fn() -> a) -> Option(a) {
case value, requirement {
Some(value), _ -> Some(value)
None, True -> Some(consequence())
_, _ -> None
}
}
fn else_then(value: Option(a), consequence: fn() -> a) -> Option(a) {
case value {
Some(value) -> Some(value)
None -> Some(consequence())
}
}
pub fn main() {
if_then(False, fn() {
io.println("Foo")
}) |> elif_then(True, fn() {
io.println("Bar")
}) |> else_then(fn() {
io.println("Baz")
})
} |
Beta Was this translation helpful? Give feedback.
-
I was reading code like this:
Because I came from Erlang and Elixir, I could consider something that's clear to keep it as Erlanger but maybe it could be simplified (my alchemist is yelling about it! :-D ):
Of course, it should be useful and used for Bool evaluations, what do you think?
Beta Was this translation helpful? Give feedback.
All reactions