fn main() {
num := 777
s := if num % 2 == 0 {
'even'
}
else {
'odd'
}
println(s) // ==> "odd"
}
Of course you can have multiple statements in each branch:
fn main() {
num := 777
s := if num % 2 == 0 {
println('first branch')
'even'
}
else {
println('second branch')
'odd'
}
println(s) // ==> "odd"
}
Why confused, @arhen?
This is great. Perhaps it could be generalised to for-loops and switches. Maybe something like this:
fn main() {
numbers := [1, 2, 3, 4, 5]
squares := for n in numbers { n * n }
}
This would be a nice way of emulating the map and list comprehensions (since a conditional could be included inside the body.) Though, if it was required that an element wouldn't have to be accumulated on each iteration of the loop, a keyword could be introduced similar to Python's yield.
Good idea, @zac-garby.
Not a fan of introducing a new keyword though. Would like to keep the total amount of keywords under 25.
V will have lambdas, map, and filter soon.
switch will be replaced with a match expression.
That's fair enough. Also, awesome, I much prefer match over switch.
Instead of yield, what could happen is that an element will automatically be returned in the output array unless a break statement is issued.
V will have lambdas, map, and filter soon.
I've done some design work on generic algorithms for V, including map. It takes an iterator and returns another iterator, no allocations are made. I'll post an issue about it soon, just making some tweaks.
Can the return at the end of a function be omitted like this?
Can the return at the end of a function be omitted like this?
No. I think for consistency and simplicity explicit returns must be required.
Most helpful comment
No. I think for consistency and simplicity explicit returns must be required.