wgsl-tooling-wg / wesl-spec

A portable and modular superset of WGSL
BSD 3-Clause "New" or "Revised" License
36 stars 3 forks source link

Algebraic Data Types #59

Open k2d222 opened 1 month ago

k2d222 commented 1 month ago

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
    }
}
k2d222 commented 1 month ago

related: #58 #34 #40