Elan-R
Elan-R

Reputation: 554

How to parametrically check if a variable is of a type in Mojo?

I am trying to create a function that takes in any object and prints it if it is _Printable, and prints "object" otherwise.

To do this I am trying to use @paramter if to check the type at compile time to choose the right printing code:

@always_inline
fn try_print[Type: AnyType](obj: Type):
    @parameter
    if isinstance(obj, _Printable):
        print_no_newline(obj)
    else:
        print_no_newline("object")

But it seems the way to check types in Mojo is different than Python because it throws an error saying isinstance does not exist, and I cannot find a way to do it in Mojo on their online module directory or programming guides.

Essentially: What is Mojo's alternative to Python's isinstance?

Upvotes: 5

Views: 406

Answers (0)

Related Questions