This Week in Mojo 2023-07-14

Official Content

Mojo Playground Update

⭐️ New

  • Mojo now has support for global variables! This enables var and let declaration at the top-level scope in Mojo files. Global variable initializers are run when code modules are loaded by the platform according to the order of dependencies between global variables, and their destructors are called in the reverse order.

  • The Mojo programming manual is now written as a Jupyter notebook, and available in its entirety in the Mojo Playground programming-manual.ipynb. Previously, HelloMojo.ipynb included most of the same material, but it was not up-to-date. We’ve also re-written HelloMojo.ipynb to be much shorter and provide a more gentle first-user experience.

  • Coroutine module documentationopen in new window is now available. Coroutines form the basis of Mojo’s support for asynchronous execution. Calls to async fn can be stored into a Coroutine, from which they can be resumed, awaited upon, and have their results retrieved upon completion.

🦋 Changed

  • simd_bit_width in the TargetInfo module has been renamed to simdbitwidth to better align with simdwidthof, bitwidthof, etc.

🛠️ Fixed

  • The walrus operator now works in if/while statements without parentheses, such as x := function():

  • Issue #428open in new window: The FloatLiteral and SIMD types now support conversion to Int via the to_int or __int__ method calls. The behavior matches that of Python, which rounds towards zero.

Mojo Team Answers

Global Variables REPL

Global variables were added to the language but they have not been wired into the REPL environment yet. The REPL environment layers extra features on the language to provide redefinition and top-level variables, and using global variables to enable the code you wrote has not occurred yet. Sorry for the confusion!