Skip to content

Switch starter.mojo kernels and solve from fn to def#240

Merged
kunal-mansukhani merged 1 commit intomainfrom
kunal/mojo-fn-to-def
Apr 5, 2026
Merged

Switch starter.mojo kernels and solve from fn to def#240
kunal-mansukhani merged 1 commit intomainfrom
kunal/mojo-fn-to-def

Conversation

@kunal-mansukhani
Copy link
Copy Markdown
Contributor

Summary

Mojo 26.2 deprecates the `fn` keyword per the RFC "Remove fn from Mojo, def no longer implies raises" (accepted 23 Feb 2026). Going forward:

  • `def foo(...)` — non-raising (was `fn`)
  • `def foo(...) raises` — raising form (was `fn ... raises`, replaces the old implicitly-raising `def`)

`fn` will be removed before 1.0. Every starter currently emits a deprecation warning on compile. This sweeps all 79 `starter.mojo` files to the new form now so learners don't see the dead keyword.

Mapping

before after
`fn vector_add_kernel(...)` `def vector_add_kernel(...)`
`@export fn solve(...) raises:` `@export def solve(...) raises:`

Kernels drop to plain `def` — they don't raise. `solve` keeps the `raises` because it calls `DeviceContext`, `compile_function`, `enqueue_function`, and `synchronize`, all of which raise.

Test plan

  • Transformed `vector_add` starter compiles cleanly on Mojo 26.2 via the prod compiler container (`mojo build --emit=shared-lib --target-accelerator=sm_75` → exit 0, exported `solve` symbol in the `.so`).
  • CI lint / pre-commit passes.

🤖 Generated with Claude Code

Mojo 26.2 deprecates the `fn` keyword. Per the RFC "Remove fn from
Mojo" (accepted 23 Feb 2026), `def` is becoming Mojo's single function
declaration keyword:

- `def foo(...)` — non-raising (matches old `fn`)
- `def foo(...) raises` — raising form (replaces old `fn ... raises`
  and the old implicitly-raising `def`)

`fn` will be removed before 1.0. Starters emit a deprecation warning
on every compile today; refactor to the new form now so users don't
learn the dead keyword.

Kernels become `def` without `raises` (they don't raise). `solve`
becomes `def ... raises` since it calls DeviceContext, compile_function,
enqueue_function, and synchronize — all of which raise.

Verified against Mojo 26.2 on a Tesla T4: the transformed vector_add
starter compiles cleanly and produces a valid shared library.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kunal-mansukhani kunal-mansukhani merged commit ca891d3 into main Apr 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant