How to create a generic list display function in Mojo? #3318
-
This is my idea: from collections import List
fn print_list[T: CollectionElement](list: List[T]):
for element in list:
print(element[], end=" ")
def main():
list = List(1,5,3,2,6)
print_list(list) But it's not work: /media/lucifer/STORAGE/IMPORTANT/test/test.mojo:5:14: error: no matching function in call to 'print'
print(element[], end=" ")
~~~~~^~~~~~~~~~~~~~~~~~~~
/media/lucifer/STORAGE/IMPORTANT/test/test.mojo:1:1: note: candidate not viable: unknown keyword argument: 'end'
from collections import List
^
/media/lucifer/STORAGE/IMPORTANT/test/test.mojo:1:1: note: candidate not viable: missing 1 required keyword-only parameter: 'EndTy'
from collections import List
^
/media/lucifer/STORAGE/IMPORTANT/test/test.mojo:1:1: note: candidate not viable: missing 1 required keyword-only argument: 'sep'
from collections import List
^
/media/lucifer/STORAGE/IMPORTANT/test/test.mojo:1:1: note: candidate not viable: missing 1 required keyword-only argument: 'sep'
from collections import List
^
/home/lucifer/.modular/pkg/packages.modular.com_mojo/bin/mojo: error: failed to parse the provided Mojo source module The error message is a bit confusing. Anyone can help? |
Beta Was this translation helpful? Give feedback.
Answered by
Lucifer-02
Aug 5, 2024
Replies: 1 comment 1 reply
-
This worked but not very nice: from collections import List
fn print_list[T: DType](list: List[Scalar[T]]):
for element in list:
print(element[], end=", ")
def main():
list = List[Float64](
1, 5, 3, 2, 6
) # Equivalent to List[Scalar[DType.float64]](1,5,3,2,6)
print_list[DType.float64](list) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another way: