-
Notifications
You must be signed in to change notification settings - Fork 3
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 Overloads #58
Comments
I wonder if we could simplify this by using generic constraints? Like instead of resolving types and using inference, we could match on the generic arguments? |
aah, do you mean something roughly like that, with type algebra? fn abs<S: AbstractFloat, T: S || vec3<S>>(e: T) -> T {
@if(T == Vec3<S>) {
// implementation with vec3
}
@if(T == S) {
// implementation with scalars
}
} I love this one. It expresses exactly what's in the spec. |
Might be worth opening another issue for algebraic data types and type constraints |
I was thinking more overloads still but that the matching is done on the generic constraints |
But that is a cool idea too! |
Do you have a use case in your own shader development for function overloads? I'd suggest we make a separate bug for the use case, and we can work with @sdedovic to see if it's the same as the one lygia describes (or make a separate case for the lygia issue). That'll give us some concrete examples to check our potential designs against. |
Function overloads allow redefining a function with different input/output parameters. It is nice to reuse the same function name for different parameters, e.g. implementing a function for f32, vec2, vec3, vec4... most of the numeric built-in functions do that.
What the spec does
WGSL spec has a concept of overload, which manifests itself in functions via function overloads and type parameters.
Function overloads are variants of the same function that take a different number of arguments or template parameters.
Function type parameters are similar-looking to generics, except that they are overloads, meaning each possible variant is implemented separately.
How we could implement it
This proposal is connected to generics, but is distinct. To support this, a WESL linker would need to implement the overload resolution algorithm which roughly is this:
Side-Quest: why are "type parameters" not just generics? Why do we need overloads?
Here's how it's implemented with overloads:
How could you implement
abs
in one go for bothvec<T>
andT
? You can't, even with generics, unless you write complex traits that allow scalars to behave like vectors. See this example with module generics:...and then implement the concretizations...
... and finally, usage of abs()...
This is nicely extensible but terribly, terribly verbose!
The text was updated successfully, but these errors were encountered: