r/ProgrammingLanguages • u/TheUltimateAsh • Jan 10 '26
Help Adding Overloadable Functions to an Interpreted Language
I followed the lovely Crafting Interpreters book by Robert Nystrom to make a basic interpreted language for a game I am developing. I added typing, so I had to change some of the grammar and so on. I implement typing as the pass right before the interpreter execution, and right after the resolution pass.
I am currently trying to add functionality for function name overloading, but I am encountering difficulty in the resolution pass. Because the resolution pass does not touch types, nor do I want it to, it raises a compilation error when two functions of the same name are declared. Since the function called in a function call should depend on the types (e.g., a function signature in a closer scope may not match the arguments being passed), I am unsure how to proceed.
The only way I can conceive of modifying the resolver to allow for function overloading is to combine the resolution and typing passes, but this violates the single responsibility principle and I am trying to avoid it.
Any thoughts or ideas?
Thanks.
3
u/srvhfvakc Jan 10 '26
Seems like the only way to do this would be to defer the compilation error when two same-name functions are declared to the typing pass. Since the only way to distinguish the two functions would be (by definition) their type.
1
u/Germisstuck CrabStar Jan 10 '26
What my language will do, is something kinda different, but for function (and by extension, operator) overloading is handled with behavior being completely separate from data. Then by using control flow, determine which logic set is the currently used one (this sounds like hell to infer, so in my language you have to explicitly say something like "let x = new Counter() with CustomCounterLogic". With this, you can just change the logic in different contexts easily, allowing for using existing names.
I'm not sure how useful this would be to your language, but that's just what I'm doing.
1
u/AustinVelonaut Admiran Jan 10 '26
Another way to handle this would be to mark overloaded names distinct from non-overloaded names, with e.g. a declaration like
generic print;, then when you come to name resolution, defer any resolution for names that are marked asgenericuntil the typecheck phase.1
u/[deleted] Jan 10 '26
[removed] — view removed comment