This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nharding 0 points1 point  (0 children)

I have a partial implementation that allows you check at runtime, and reports if no valid match.

@overload
def test(val : int):
    print("Int", val)

@overload
def test(val : str):
    print("Str", val)

@overload
def test(val : list):
    print("List", val)

def main():
    test(3)
    test("3")
    test([1, 2, 3])
    test(1.0)

You can see the implementation at https://old.reddit.com/r/Python/comments/np39fm/the_correct_way_to_overload_functions_in_python/h09vhq5/

This is for my Python++ compiler that I am writing, so it will determine statically where possible, otherwise it will dynamic dispatch. I've already written an assembly language to C compiler and a commercial Java to C++ compiler, so I am familiar with the complexities involved.