-
-
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
Generic function as types #21066
Comments
What's wrong with these signatures?
|
No language proposals please |
This proposal has a few issues. The first is that it makes generic functions non-obvious. Today, Zig has the nice property that a function is generic precisely if it has The second, and much more important, issue, is that what you are proposing isn't actually possible. The fact that I was about to say I'll close this, but Andrew beat me to it :^) |
I think I found that generic types are pretty fine, but when you need to not have a specific generic type, it just becomes a
anytype
rabbit hole,anytype
is fine as is, but when using other person's libraries that uses them, it can become quite a bad design pattern if you need to have a really generic type. Right now interfaces likeAllocator
orWriter
(Writer
is deprecated, so i'm talking aboutAnyWriter
) are plain structs with no generic patterns, so instead of having dynamic dispatched interfaces, we could have static ones, which can be faster and optimized in more ways (discard, inline call and everything else involving function optimization), with "more generic functions".Full though process
Let's take:
Here a
GenericType
is a blank, boring generic function, let's say "I want to use it for a function which can print it's value:"Now the problem is that the "subtype" is restricted to
u32
, what you could do to fix it is to useanytype
:"Nice, but wait... now
gt
can be anything, an int, float or opaque, crap, now i need to check the type ofgt
:""Mmh.. looks nice- wait, what if I need to modify
GenericType
in the future ? ok i'll do a function to check if a type matches:"GenericType
:and i'll use it in my
printGT
function:This sure works, but it's very tedious and still have a problem: struct literals, this could be fixed with even more code, but you get it, there is even more boilerplate and checks to do in order for it to work as intended. This could be done in another, very intuitive, way: with Generic function types.
Generic function types as function parameters
Now function parameters can accept generic function which returns types and are completely compile time, to act as a type. They acts as
anytype
but with further type checking that the compiler can do when the function gets called. Here is a use example:We can now easily extend our
GenericType
function:And our
printGT
function can just be:This could also be used for
Allocator
:They could be used for
Writer
too, as shown above.Generic function instantiation and function casting
One thing that could be interesting with it, is generic function instantiation, i.e: given a generic function, generates a new, non-generic function, implementing it as function casting could be used to cast non-extern function to extern (if the type of the function to cast could match the target function type):
This proposal could be integrated along decl literals nicely by guessing the result type (if the generic type is a simple return with a type). Struct literals could be used by also guessing the type in the same way. If you decide to change
Allocator
in the standard library, this will surely break pretty much all existing code, but having this could benefits in new projects/code and add some level of safety and predictability to the code using it.The text was updated successfully, but these errors were encountered: