新增功能:
SIMD 类型现在默认为该类型的体系结构 SIMD
宽度。这意味着您可以编写等价于 .SIMD[DType.float32]``SIMD[DType.float32, simdwidthof[DType.float32]()]
SIMD
类型现在包含一个函数,该函数允许您将两个值连接在一起并生成一个新值。join()
SIMD
SIMD
Mojo 现在除了支持关键字参数外,还支持编译时关键字参数。例如:
fn foo[a: Int, b: Int = 42]():
print(a, "+", b)
foo[a=5]() # prints '5 + 42'
foo[a=7, b=13]() # prints '7 + 13'
foo[b=20, a=6]() # prints '6 + 20'
结构中也支持关键字参数:
fn __init__(inout self):
print(msg, a)
fn use_kw_params():
KwParamStruct[a=42]() # prints '🔥mojo🔥 42'
KwParamStruct[5, msg="hello"]() # prints 'hello 5'
KwParamStruct[msg="hello", a=42]() # prints 'hello 42'
目前,存在以下值得注意的限制:
尚不支持仅关键字参数:
fn baz[*args: Int, b: Int](): pass # fails
fn baz[a: Int, *, b: Int](): pass # fails
尚不支持可变参数关键字参数:
fn baz[a: Int, **kwargs: Int](): pass # fails
Mojo 现在支持函数的“自动”参数化。这意味着,如果函数参数类型是参数化但没有绑定参数,则它们会自动添加为函数的输入参数。这适用于现有功能,允许您使用较少的样板编写参数函数。
@value
struct Thing[x: Int, y: Int]:
pass
fn foo(v: Thing):
print(v.x)
print(v.y)
fn main():
let v = Thing[2, 3]()
foo(v)
但是,尚不支持部分自动参数化:
fn foo(v: Thing[y=7]): # Partially bound type not allowed yet.
...
使用括号语法调用时,支持关键字参数传递:getitem
@value
struct MyStruct:
fn __getitem__(self, x: Int, y: Int, z: Int) -> Int:
return x * y + z
MyStruct()[z=7, x=3, y=5] # returns 22
但是,尚不支持使用括号语法传递关键字参数:__setitem__
@value
struct OtherStruct:
fn __setitem__(self, x: Int, y: Int): pass
OtherStruct()[x=1] = 4 # fails
现在可以在函数的签名中引用函数参数输入参数:
fn foo(x: SIMD, y: SIMD[x.type, x.size]):
pass
基准测试模块已得到简化和改进,因此您现在可以运行:
import benchmark
from time import sleep
fn sleeper():
sleep(.01)
fn main():
let report = benchmark.run[sleeper]()
print(report.mean())
它不再需要捕获,因此可以对同一范围之外的功能进行基准测试。fn
您可以使用以下命令打印报告:
report.print()
---------------------
Benchmark Report (s)
---------------------
Mean: 0.012314264957264957
Total: 1.440769
Iters: 117
Warmup Mean: 0.0119335
Warmup Total: 0.023866999999999999
Warmup Iters: 2
Fastest Mean: 0.012227958333333334
Slowest Mean: 0.012442699999999999
所有函数的单位默认为秒,但可以通过以下方式更改:
from benchmark import Unit
report.printUnit.ms
Mojo现在支持部分绑定类型的结构参数推导(又名.class模板参数推导或CTAD)。结构参数推导也可以从静态方法中扣除。例如:
@value
struct Thing[v: Int]: pass
struct CtadStructWithDefault[a: Int, b: Int, c: Int = 8]:
fn init(inout self, x: Thing[a]):
print("hello", a, b, c)
@staticmethod
fn foo(x: Thing[a]):
print("🔥", a, b, c)
_ = CtadStructWithDefault[b=7](Thing[6]()) # prints 'hello 6 7 8'
CtadStructWithDefault[b=7].foo(Thing[6]()) # prints '🔥 6 7 8'
Tensor 有新的方法可以从文件中保存和加载为字节。fromfile()tofile()
内置函数现在适用于 `Tensor` 类型。`print()`
`TensorShape` 和 `TensorSpec` 现在具有采用 `DynamicVector[Int]` 和 `StaticIntTuple` 来初始化形状的构造函数。
`String` 类型现在具有 和 方法,用于计算出现次数或查找字符串中子字符串的偏移索引。`count()find()`
该类型现在有一个方法,允许您将子字符串替换为另一个字符串。`Stringreplace()`