mojo_hub 有三种方法可以做到这一点: 使用类型的默认参数Optional fn f(a: Optional[Int] = None): 默认参数 fn f(a: Int = default_value): 超载 fn f(): f(default_value) fn f(a: Int): 我们没有 Python的可变默认问题,因此在许多情况下没有必要使用可选类型。
mojo_hub mojo_hub 多谢。但我怎样才能检查它是否没有呢? from collections.optional import Optional def main(): test(3) fn test(item0: Int, item1: Optional[Int] = None): print(item1.value())
mojo_hub mojo_hub 我找到了解决办法。这有点棘手,但了解一下很酷: from collections.optional import Optional def main(): test(3) # does not print anything test(3, 0) # prints 0 test(3, 5) # prints 5 fn test(item0: Int, item1: Optional[Int] = None): if item1: print(item1.value())