let base_pose = switch lowerBodyState {
case let .idle(entered):
assets.get_anim(name: "idle").pose(time: now - entered)
case let .moving(entered):
let bla = "moving"
assets.get_anim(name: bla).pose(time: now - entered)
}
return base_pose
When I try to have multiple expressions, it does not compile.
\swifting\Sources\animation.swift:92:60: error: non-expression branch of 'switch' expression may only end with a 'throw'
assets.get_anim(name: bla).pose(time: now - entered)
If I wrap the expressions in a function it compiles but this is extremely clunky.
This is a switch expression, not a statement. Currently, switch expressions do not support cases with multiple statements. As you note, you can refactor the multiple statements into a function. You can also refactor the code to use a switch statement by writing something like:
let base_pose: <<type>>
switch lowerBodyState {
case let .idle(entered):
base_pose = assets.get_anim(name: "idle").pose(time: now - entered)
case let .moving(entered):
let bla = "moving"
base_pose = assets.get_anim(name: bla).pose(time: now - entered)
}
The body of each case must contain at least one executable statement.
I believe "at least" means that multiple statements are allowed.
But
Like if statements, switch statements also have an expression form
...
You can use switch expressions on the right-hand side of an assignment
Apparently you are using the expression form of the switch keyword. As you are using a value returned by the switch expression, every case should calculate that value. Multiple statements do not return a single value, this is why you get the error message.