Skip to content

Implement generics #154

Open
Open

Description

@certik

We need to allow writing generic functions (templates) and template constraints.

One idea that we can explore is to also support something like: #152 (comment):

>>> def str(val: i32 | f64):
...   # "isinstance" type guard
...   if isinstance(val, i32):
...     # type of `val` is narrowed to `i32`
...     return str_int(val)
...   elif isinstance(val, f64):
...     # else, type of `val` is narrowed to `f64`
...     return str_float(val)

Activity

  1. Smit-create commented on Mar 13, 2022

    @Smit-create
    Collaborator

    This should now be possible with overload decorator.

  2. certik commented on Mar 13, 2022

    @certik
    ContributorAuthor

    This particular case can be. We still need generics/templates.

    The overload in a sense is a subset of generics: you provide specific implementations ahead of time. The templates do not provide specific implementations, the compiler instantiates them.

  3. Aser-Abdelfatah commented on Sep 24, 2024

    @Aser-Abdelfatah

    Was this picked up by any person? Or is it still open for contribution?

  4. certik commented on Sep 25, 2024

    @certik
    ContributorAuthor

    Yes, it's still open. We have quite solid generics in libasr thanks to LFortran, and we should hook them into LPython.

  5. Aser-Abdelfatah commented on Nov 7, 2024

    @Aser-Abdelfatah

    Hi,
    I'm doing more research into this. I found some generics implemented in GSoC 2022. I'd just appreciate if there were more details on which generics have higher priority or where to start

  6. certik commented on Nov 8, 2024

    @certik
    ContributorAuthor

    They are implemented in LFortran, you can start here: https://docs.lfortran.org/en/asr/generics/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions