-
Notifications
You must be signed in to change notification settings - Fork 171
Implement Overload decorator in pure python #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from ltypes import overload | ||
|
||
@overload | ||
def foo(a: int, b: int) -> int: | ||
return a*b | ||
|
||
@overload | ||
def foo(a: int) -> int: | ||
return a**2 | ||
|
||
@overload | ||
def foo(a: str) -> str: | ||
return "lpython-" + a | ||
|
||
@overload | ||
def test(a: int) -> int: | ||
return a + 10 | ||
|
||
@overload | ||
def test(a: bool) -> int: | ||
if a: | ||
return 10 | ||
return -10 | ||
|
||
|
||
assert foo(2) == 4 | ||
assert foo(2, 10) == 20 | ||
assert foo("hello") == "lpython-hello" | ||
assert test(10) == 20 | ||
assert test(False) == -test(True) and test(True) == 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,53 @@ | ||
from inspect import getfullargspec, getcallargs | ||
|
||
# data-types | ||
|
||
i32 = [] | ||
i64 = [] | ||
f32 = [] | ||
f64 = [] | ||
c32 = [] | ||
c64 = [] | ||
|
||
# overloading support | ||
|
||
class OverloadedFunction: | ||
""" | ||
A wrapper class for allowing overloading. | ||
""" | ||
global_map = {} | ||
|
||
def __init__(self, func): | ||
self.func_name = func.__name__ | ||
f_list = self.global_map.get(func.__name__, []) | ||
f_list.append((func, getfullargspec(func))) | ||
self.global_map[func.__name__] = f_list | ||
|
||
def __call__(self, *args, **kwargs): | ||
func_map_list = self.global_map.get(self.func_name, False) | ||
if not func_map_list: | ||
raise Exception("Function not defined") | ||
for item in func_map_list: | ||
func, key = item | ||
try: | ||
# This might fail for the cases when arguments don't match | ||
ann_dict = getcallargs(func, *args, **kwargs) | ||
except TypeError: | ||
continue | ||
flag = True | ||
for k, v in ann_dict.items(): | ||
if not key.annotations.get(k, False): | ||
flag = False | ||
break | ||
else: | ||
if type(v) != key.annotations.get(k): | ||
flag = False | ||
break | ||
if flag: | ||
return func(*args, **kwargs) | ||
raise Exception("Function not found with matching signature") | ||
|
||
|
||
def overload(f): | ||
overloaded_f = OverloadedFunction(f) | ||
return overloaded_f |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the arguments don't match, shouldn't we rather raise an exception that the arguments don't match?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose, the list contains two functions
foo(a)
andfoo(a, b)
, then,foo(1, 2)
will raise an error if we usegetcallargs
on the first function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. We should determine which of the overloads contains two integer arguments in this case. And just call that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right!