Optional function argument?
Hi everyone
I'm wondering if it's possible to have an optional argument for functions in Mojo. So then if an argument were given, it would have a value (int for instance) otherwise, it would be None.
3 Replies
Congrats @Arvin_David, you just advanced to level 1!
There are three ways to do this:
- default argument using the
Optional
type
- default argument
- overload
We don't have Python's mutable default problem, so using a Optional type is unnecessary in many cases.Thanks a lot. But how can I also check if it's none or not?
For example in the code above, I wonder why the printed value is 0. As far as I know, we cannot print None at the moment, So is mojo converting None to 0, or None was initially treated like zero? How can I check if it's originally None or not?
I found the solution. It's a little bit tricky but cool to know: