You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
typeT=string|number// union (one OR the other)typeU={foo: string}&{bar: T}// intersection (one AND the other) { foo: string, bar: string | number }
Syntax in Rust
// union (one OR the other)enumStringOrNumber{String(String),Number(u32)}// intersection (one AND the other)structFooAndBar{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}}
The text was updated successfully, but these errors were encountered:
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):
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:
The text was updated successfully, but these errors were encountered: