algebraic data types are a feature of many modern languages, including rust and typescript. They allow to express the union or intersection of types.
Syntax in Typescript
type T = string | number // union (one OR the other)
type U = { foo: string } & { bar: T } // intersection (one AND the other) { foo: string, bar: string | number }
Syntax in Rust
// union (one OR the other)
enum StringOrNumber { String(String), Number(u32) }
// intersection (one AND the other)
struct FooAndBar { foo: String, bar: StringOrNumber }
Possible syntax in WESL
syntactically, no changes need to be made to WGSL to support this, which is really neat. WGSL already has Type Expressions, so the following is valid (only syntactically):
vec3<f32> | f32
myStruct & myOtherStruct
What use-cases?
there is only one location where types are valid expressions in WGSL (afaik): in templates to define generic types. array<here>, vecN<here>, ptr<here>...
in WESL we plan on allowing user-defined generic functions. With ADTs, the following would be possible:
fn abs<S: AbstractFloat, T: S | vec3<S>>(e: T) -> T {
@if(T == Vec3<S>) {
// implementation with vec3
}
@if(T == S) {
// implementation with scalars
}
}
algebraic data types are a feature of many modern languages, including rust and typescript. They allow to express the union or intersection of types.
Syntax in Typescript
Syntax in Rust
Possible syntax in WESL
syntactically, no changes need to be made to WGSL to support this, which is really neat. WGSL already has Type Expressions, so the following is valid (only syntactically):
What use-cases?
there is only one location where types are valid expressions in WGSL (afaik): in templates to define generic types.
array<here>
,vecN<here>
,ptr<here>
...in WESL we plan on allowing user-defined generic functions. With ADTs, the following would be possible: