Crazy idea (RFC): Function signature bindings #2023
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
Related to #1717
Short
Extend
type
with a new variant and properties for function signatures. Create a new type-builder expressionfnsig
yielding this new type variant. Afnsig
describes the names and types of parameters and the return type.Motivation
Consider this example of writing a program to dynamically execute a function from an array of function pointers (dispatch table).
Here,
MathFunc
is a binding of the type pointer to function of i32 => i32. As shown, function pointers can already be bound to a name.Let's observe a more complicated example.
For every function from the dispatch table the entire function signature has to be typed out again every time, even though these functions are related and share the same signature. This creates a lot of redundant code.
With function signature bindings, the second example could be rewritten as
Downsides
Obviously the usage of a binding completely hides the parameter types and names and return type. Inside the function body the parameter names would be derived from the names in the fnsig. This means bindings with anonymous parameters (
const Foo = fnsig(i32, bool) u8
) are not possible.Less crazy
Here is a more useful instance of this feature, for declaring external functions.
Not invented here
Good old C actually has this feature. Well, the less crazy variant at least. Most people are aware of function pointer aliases.
But C also has function type aliases.
The text was updated successfully, but these errors were encountered: