v0.3.0 (2023-09-21)
There's more Mojo to love in this, the second release of the Mojo SDK! This release includes new features, an API change, and bug fixes.
There's also an updated version of the Mojo extension for VS Code.
⭐️ New
-
Mojo now has partial support for passing keyword arguments to functions and methods. For example the following should work:
fn foo(a: Int, b: Int = 3) -> Int:
return a * b
fn main():
print(foo(6, b=7)) # prints '42'
print(foo(a=6, b=7)) # prints '42'
print(foo(b=7, a=6)) # prints '42'Parameters can also be inferred from keyword arguments, for example:
fn bar[A: AnyType, B: AnyType](a: A, b: B):
print("Hello 🔥")
fn bar[B: AnyType](a: StringLiteral, b: B):
print(a)
fn main():
bar(1, 2) # prints `Hello 🔥`
bar(b=2, a="Yay!") # prints `Yay!`For the time being, the following notable limitations apply:
-
Keyword-only arguments are not supported:
fn baz(*args: Int, b: Int): pass # fails
fn baz(a: Int, *, b: Int): pass # fails(Keyword-only arguments are described in PEP 3102.)
-
Variadic keyword arguments are not supported:
fn baz(a: Int, **kwargs: Int): pass # fails
-
-
Mojo now supports the
@nonmaterializabledecorator. The purpose is to mark data types that should only exist in the parameter domain. To use it, a struct is decorated with@nonmaterializable(TargetType). Any time the nonmaterializable type is converted from the parameter domain, it is automatically converted toTargetType. A nonmaterializable struct should have all of its methods annotated as@always_inline, and must be computable in the parameter domain. In the following example, theNmStructtype can be added in the parameter domain, but are converted toHasBoolwhen materialized.@value
@register_passable("trivial")
struct HasBool:
var x: Bool
fn __init__(x: Bool) -> Self:
return Self {x: x}
@always_inline("nodebug")
fn __init__(nms: NmStruct) -> Self:
return Self {x: True if (nms.x == 77) else False}
@value
@nonmaterializable(HasBool)
@register_passable("trivial")
struct NmStruct:
var x: Int
@always_inline("nodebug")
fn __add__(self: Self, rhs: Self) -> Self:
return NmStruct(self.x + rhs.x)
alias stillNmStruct = NmStruct(1) + NmStruct(2)
# When materializing to a run-time variable, it is automatically converted,
# even without a type annotation.
let convertedToHasBool = stillNmStruct -
Mojo integer literals now produce the
IntLiteralinfinite precision integer type when used in the parameter domain.IntLiteralis materialized to theInttype for runtime computation, but intermediate computations at compile time, using supported operators, can now exceed the bit width of theInttype. -
The Mojo Language Server now supports top-level code completions, enabling completion when typing a reference to a variable, type, etc. This resolves #679.
-
The Mojo REPL now colorizes the resultant variables to help distinguish input expressions from the output variables.
🦋 Changed
-
Mojo allows types to implement two forms of move constructors, one that is invoked when the lifetime of one value ends, and one that is invoked if the compiler cannot prove that. These were previously both named
__moveinit__, with the following two signatures:fn __moveinit__(inout self, owned existing: Self): ...
fn __moveinit__(inout self, inout existing: Self): ...We've changed the second form to get its own name to make it more clear that these are two separate operations: the second has been renamed to
__takeinit__:fn __moveinit__(inout self, owned existing: Self): ...
fn __takeinit__(inout self, inout existing: Self): ...The name is intended to connote that the operation takes the conceptual value from the source (without destroying it) unlike the first one which "moves" a value from one location to another.
For more information, see the Mojo Manual section on move constructors.
-
The Error type in Mojo has changed. Instead of extracting the error message using
error.valueyou will now extract the error message usingerror.message().
🛠️ Fixed
- #503 - Improve error message
for failure lowering
kgen.param.constant. - #554 - Alias of static tuple fails to expand.
- #500 - Call expansion failed due to verifier error.
- #422 - Incorrect comment detection in multiline strings.
- #729 - Improve messaging on how to exit the REPL.
- #756 - Fix initialization errors of the VS Code extension.
- #575 - Build LLDB/REPL with libedit for a nicer editing experience in the terminal.