You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And we implement the generic_function decorator in ltypes.py to be used by CPython. Then LPython has understanding of this decorator and it creates GenericProcedure ASR type for sin.
The CPython implementation of the overload decorator might use some global dictionary/mapping where it stores all the overloads and then returns a sin function that will consult this dictionary to call the proper overload based on the argument. The LPython implementation would just create a GenericProcedure in ASR.
Perfect! Please submit it as a PR, put overload into ltypes and put the tests into integration_tests/test_generics_01.py. We'll also extend our test runner to skip LPython tests and only run CPython one on this test until we implement it.
rename Dummy to something more descriptive. Perhaps OverloadedFunction?
Don't use empty except. I think you don't have to use except at all there, getcallargs should not fail, or if it can, let's catch the specific exception.
Move global_map into a class variable, which is effectively still global, shared across all instances
Add type annotations for return values to the tests (foo)
Activity
certik commentedon Feb 14, 2022
So one good design might be:
And we implement the
generic_functiondecorator inltypes.pyto be used by CPython. Then LPython has understanding of this decorator and it createsGenericProcedureASR type forsin.certik commentedon Feb 22, 2022
Python has
typing.overloaddecorator: https://docs.python.org/3/library/typing.html#typing.overload, which is almost what we want. We could doltypes.overload:The CPython implementation of the
overloaddecorator might use some global dictionary/mapping where it stores all the overloads and then returns asinfunction that will consult this dictionary to call the proper overload based on the argument. The LPython implementation would just create aGenericProcedurein ASR.Smit-create commentedon Feb 23, 2022
Here is the example of python's overload:
Output:
<class 'int'> <class 'str'>For this we need to add the ellipsis (#146) both in AST and ASR.
Smit-create commentedon Feb 23, 2022
I tried implementing the
overloadoperator in pure python which works in much similar fashion using this following script:Python Script
It seems to be working fine, though it might require a bit of more testing.
certik commentedon Feb 23, 2022
Perfect! Please submit it as a PR, put
overloadintoltypesand put the tests intointegration_tests/test_generics_01.py. We'll also extend our test runner to skip LPython tests and only run CPython one on this test until we implement it.certik commentedon Feb 23, 2022
Couple comments to the implementation:
Dummyto something more descriptive. PerhapsOverloadedFunction?except. I think you don't have to useexceptat all there,getcallargsshould not fail, or if it can, let's catch the specific exception.global_mapinto a class variable, which is effectively still global, shared across all instancesfoo)Otherwise this looks great.
Smit-create commentedon Feb 26, 2022
I'm now thinking to extend the overload support to LPython.