soma.cell

E0068: return_in_lambda

E0068 return_in_lambda — return inside a lambda.

A compile-time error of soma check, the checker of the Soma programming language (soma explain E0068).

The rule

A lambda is an expression: the value of a block lambda is its last expression, and return inside it is refused because it cannot leave the enclosing handler. Write the branch as an if ... { a } else { b } expression that is the last expression of the lambda, or use a for loop in the handler when you need an early return.

A program that triggers it

wrong.cell

cell Grades {
    on label_scores(scores: List<Int>) {
        let labels = map(scores, s => {
            if s < 50 { return "fail" }
            "pass"
        })
        return labels
    }
}

What soma check prints:

$ soma check wrong.cell
error: `return` inside a block lambda — a lambda is an expression: its value is its LAST expression (`x => { let y = x * 2  y + 1 }`); it cannot leave the handler
  --> wrong.cell:4:25
  |
4 |             if s < 50 { return "fail" }
  |                         ^
  = code: E0068
✗ 1 error(s), 0 warning(s).

The fix field of soma check --json: a lambda is an expression: its value is its LAST expression (`x => { let y = x * 2 y + 1 }`); it cannot leave the handler

Lines 4–5 are rewritten.

wrong.cell → right.cell

--- wrong.cell
+++ right.cell
@@ -2,6 +2,5 @@
     on label_scores(scores: List<Int>) {
         let labels = map(scores, s => {
-            if s < 50 { return "fail" }
-            "pass"
+            if s < 50 { "fail" } else { "pass" }
         })
         return labels

With the fix, soma check accepts the program:

$ soma check right.cell
✓ All checks passed.