August 2023
2023-08-24
- Fixed issue where the
with expr as xstatement withinfnbehaved as if it were in adef, bindingxwith function scope instead of using lexical scope.
⭐️ New
-
Major refactoring of the standard library to enable packaging and better import ergonomics:
- The packages are built as binaries to improve startup speed.
- Package and module names are now lowercase to align with the Python style.
- Modules have been moved to better reflect the purpose of the underlying
functions (e.g.
Pointeris now within theunsafemodule in thememorypackage). - The following modules are now included as built-ins:
SIMD,DType,IO,Object, andString. This means it's no longer necessary to explicitly import these modules. Instead, these modules will be implicitly imported for the user. Private methods within the module are still accessible using thebuiltin.module_name._private_methodimport syntax. - New
mathpackage has been added to contain thebit,math,numerics, andpolynomialmodules. The contents of themath.mathmodule are re-exported into themathpackage.
-
Mojo now supports using memory-only types in parameter expressions and as function or type parameters:
@value struct IntPair: var first: Int var second: Int fn add_them[value: IntPair]() -> Int: return value.first + value.second fn main(): print(add_them[IntPair(1, 2)]()) # prints '3' -
In addition, Mojo supports evaluating code that uses heap-allocated memory at compile-time and materializing compile-time values with heap-allocated memory into dynamic values:
fn fillVector(lowerBound: Int, upperBound: Int, step: Int) -> DynamicVector[Int]: var result = DynamicVector[Int]() for i in range(lowerBound, upperBound, step): result.push_back(i) return result fn main(): alias values = fillVector(5, 23, 7) for i in range(0, values.__len__()): print(values[i]) # prints '5', '12', and then '19'
🦋 Changed
-
def main():, without the explicitNonetype, can now be used to define the entry point to a Mojo program. -
The
assert_paramfunction has been renamed toconstrainedand is now a built-in function. -
The
printfunction now works onComplexvalues.
🛠️ Fixed
- Fixed issues with print formatting for
DType.uint16andDType.int16. - Issue #499 - Two new
rotate_rightandrotate_leftfunctions have been added to the SIMD module. - Issue #429 - You can now
construct a
Boolfrom aSIMDtype whose element-type isDType.bool. - Issue #350 - Confusing Matrix implementation
- Issue #349 - Missing load_tr in struct Matrix
- Issue #501 - Missing syntax error messages in Python expressions.
2023-08-09
🦋 Changed
-
The
refandmutrefidentifiers are now treated as keywords, which means they cannot be used as variable, attribute, or function names. These keywords are used by the "lifetimes" features, which is still in development. We can consider renaming these (as well as other related keywords) when the development work gels, support is enabled in public Mojo builds, and when we have experience using them. -
The argument handling in
deffunctions has changed: previously, they had special behavior that involved mutable copies in the callee. Now, we have a simple rule, which is thatdefargument default to theownedconvention (fnarguments still default to theborrowedconvention).This change is mostly an internal cleanup and simplification of the compiler and argument model, but does enable one niche use-case: you can now pass non-copyable types to
defarguments by transferring ownership of a value into thedefcall. Before, that would not be possible because the copy was made on the callee side, not the caller's side. This also allows the explicit use of theborrowedkeyword with adefthat wants to opt-in to that behavior.
2023-08-03
⭐️ New
-
A new
Tensortype has been introduced. This tensor type manages its own data (unlikeNDBufferandBufferwhich are just views). Therefore, the tensor type performs its own allocation and free. Here is a simple example of using the tensor type to represent an RGB image and convert it to grayscale:from tensor import Tensor, TensorShape from utils.index import Index from random import rand let height = 256 let width = 256 let channels = 3 # Create the tensor of dimensions height, width, channels and fill with # random value. let image = rand[DType.float32](height, width, channels) # Declare the grayscale image. var gray_scale_image = Tensor[DType.float32](height, width) # Perform the RGB to grayscale transform. for y in range(height): for x in range(width): let r = image[y, x, 0] let g = image[y, x, 1] let b = image[y, x, 2] gray_scale_image[Index(y, x)] = 0.299 * r + 0.587 * g + 0.114 * b
🛠️ Fixed
- Issue #53 -
Intnow implements true division with the/operator. Similar to Python, this returns a 64-bit floating point number. The corresponding in-place operator,/=, has the same semantics as//=.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!