Skip to content
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

Algebraic Data Types #59

Open
k2d222 opened this issue Oct 13, 2024 · 1 comment
Open

Algebraic Data Types #59

k2d222 opened this issue Oct 13, 2024 · 1 comment

Comments

@k2d222
Copy link
Contributor

k2d222 commented Oct 13, 2024

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
    }
}
@stefnotch stefnotch changed the title Algebaric Data Types Algebraic Data Types Oct 13, 2024
@k2d222
Copy link
Contributor Author

k2d222 commented Oct 13, 2024

related: #58 #34 #40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant