Zain Raza (he/him)
Zain Raza (he/him)
MModular
Created by Zain Raza (he/him) on 12/31/2023 in #questions
Are Type Hints Still Possible in Mojo?
Hi all - I was wondering, is there a way to write a def function in Mojo that uses type hints like in Python 3? For example I tried writing this earlier. I'm guessing it's not the right approach though, as the compiler gives a warning that cannot use a dynamic value in type specification
from python import Python

def outer_func():
try:
let PythonInt = Python.import_module("typing").Int

def inner_func(x: Int): # this gives the error
...
except:
pass
from python import Python

def outer_func():
try:
let PythonInt = Python.import_module("typing").Int

def inner_func(x: Int): # this gives the error
...
except:
pass
Although in this case, I suppose it'd be a better idea to use the built-in Int type, to let others know what kind of value x is expecting?
5 replies