Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

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?

You must be logged in to vote

Another way:

from collections import List

fn printList[type: DType](list: List[Scalar[type]]):
  for e in list:
    print(e[])

def main():
 list = List[Int64](1,4,5,6)
 printList(list)

Replies: 1 comment · 1 reply

Comment options

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)
You must be logged in to vote
1 reply
@Lucifer-02
Comment options

Another way:

from collections import List

fn printList[type: DType](list: List[Scalar[type]]):
  for e in list:
    print(e[])

def main():
 list = List[Int64](1,4,5,6)
 printList(list)
Answer selected by Lucifer-02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.