lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Implement generics

Open certik opened this issue 3 years ago • 6 comments

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

One idea that we can explore is to also support something like: https://github.com/lcompilers/lpython/pull/152#issuecomment-1047082741:

>>> 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)

certik avatar Feb 21 '22 18:02 certik

This should now be possible with overload decorator.

Smit-create avatar Mar 13 '22 13:03 Smit-create

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.

certik avatar Mar 13 '22 17:03 certik

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

Aser-Abdelfatah avatar Sep 24 '24 23:09 Aser-Abdelfatah

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

certik avatar Sep 25 '24 05:09 certik

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

Aser-Abdelfatah avatar Nov 07 '24 23:11 Aser-Abdelfatah

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

certik avatar Nov 08 '24 01:11 certik