-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Function overloading – one more time #1251
Comments
No function overloading, no functions with default values (#484) |
I am not sure that C macros of C++ templates are good examples here. The first, C macros, may not be a good example because they are purely at a textual/source level and are difficult (when it is possible at all) to make typesafe etc. C++ is probably closer, but still can require you to specify the types. If I look at your Also, while it feels (and correct me if I understood this incorrectly!) that the major problem with |
To elaborate on the max example, Jai last I knew would allow you to specify which argument type deduction would be based on:
You can do left-most argument deduction with zig now as the following:
This however doesn't work since types are comptime values are evaluated left-to-right.
|
We want identifiers to refer to a single definition rather than a set of possible overloads. If we had overloading, several things would get more complicated, like taking a pointer to a function, or rules for what identifiers are allowed to be declared in what scopes, or what overload is being called for all the possible instantiations of some generic parameters. Function overloading is not necessary, and it causes lots of complexity and confusion. |
I'm not advocating full function overloading but instead observing some problems:
|
Are you proposing a solution, or just discussing the topic? |
@thejoshwolfe Discussing the topic since it appears to ripple through the entire design of the language. |
As I understand it, function overloading is a no-go (see, for example #148 ).
With extended namespaced struct functions ( #1170 ) it's possible to circumvent part of the problem somewhat, but we're likely to end up with an informal naming of parameter types through the function name e.g.
arrayCount(array)
mapCount(map)
stringCount(string)
(with #1170, we would have been able to namespace, soarray.count()
map.count()
string.count()
would all have been valid new methods)There is also the inelegance of things like
max(f32, a, b)
where type is carried around, even though it could be inferred. In this, we can contrast with Jai, wheremax(a, b)
would be generated in pretty much the same way as Zig, but would not need the type parameter.Another issue arises with the explicit type parametrization: consider complex types, rather than fundamental ones, e.g.:
count(ArrayList([]const u8), list)
... compare this to Jai'scount(list)
.I've looked through the proposals for improvements, and it feels like this is an underlying reason why many proposals get rejected.
Finally I want to mention that both using C macros or C++ templates we could easily write a
MAX(a, b)
, or acount(iterable)
function that works without passing the explicit type, but Zig, despite better compile time functionality can't do it due to missing overload.The text was updated successfully, but these errors were encountered: