codon icon indicating copy to clipboard operation
codon copied to clipboard

[Feature Request] Support function overloading outside of classes

Open AndreiMoraru123 opened this issue 1 year ago • 1 comments

I noticed codon does not mangle names, which is what canonically allows C++ to overload anything.

For example, it would be cool if I could drop the "2" from the second fn name, something I can't do in Python because it lacks static typing:

def make_fs(disk: str) -> List[int]:
    fs = []
    id = 0
    for i, x in enumerate(disk):
        x = int(x)
        if i % 2 == 0:
            fs += [id] * x
            id += 1
        else:
            fs += [-1] * x
    return fs


def make_fs2(disk: str, loc: Array[int], size: Array[int]) -> List[int]:
    fs = []
    id = 0
    for i, x in enumerate(disk):
        x = int(x)
        if i % 2 == 0:
            loc[id] = len(fs)
            size[id] = x
            fs += [id] * x
            id += 1
        else:
            fs += [-1] * x
    return fs

If I rename both functions the same make_fs and print the signature:

print(make_fs.__name__)

it seems only the last defined is registered:

make_fs[str,Array[int],Array[int]]

while if I just swap the order in which they are defined in the file, I can change the final implementation:

make_fs[str]

I understand why this makes sense, but it feels very interpreted-like, and not compiled-like.

AndreiMoraru123 avatar Dec 09 '24 18:12 AndreiMoraru123

I put the [Feature Request] tag on this so that the issue makes more sense, but ofc if this is not a request, especially if there are higher priorities =), so feel free to tag or close this

Just putting ideas on the radar

AndreiMoraru123 avatar Dec 15 '24 08:12 AndreiMoraru123

You can already do this; use @overload and it should work (see https://github.com/exaloop/codon/blob/develop/stdlib/itertools.codon for some examples).

inumanag avatar Sep 30 '25 23:09 inumanag