Stream: general

Topic: Ideas on how to represent generic types in a component mo...


view this post on Zulip Damian Reeves (Apr 06 2024 at 13:55):

Are there any recommended strategies how one might represent generics in a wit signature or in core WASM? I’d like to describe a module from a standard lib for a small programming language in wit but I know it doesn’t support custom generic types, does anyone have any hints on how I can represent and process such a thing in wit? What kind of protocol I’d need to come up with?

    combine: a -> a -> a
       Identity: a -> a

For example the above 2 functions

view this post on Zulip Dan Gohman (Apr 06 2024 at 17:12):

One approach might be to make a be a handle to a resource, and then implement dynamic typing within that resource.

view this post on Zulip Dan Gohman (Apr 06 2024 at 17:14):

But another approach is to consider that a standard lib for a language with a sophisticated type system may not be an advantageous place to use Wit, at least as long as Wit lacks custom generics.

view this post on Zulip Damian Reeves (Apr 06 2024 at 17:50):

Thanks for the suggestion

view this post on Zulip Landon James (Apr 13 2024 at 00:19):

I am using a combination of resource types and variant types to model something genericish. The container resource holds the data, it is instantiated with an enum describing the generic child type, and it returns the child data wrapped in a variant the describes the type and holds the data. Not the prettiest thing in the world, but it works for the moment.

resource container{
    constructor(input: container-input);
    get-data: func() -> container-data;
    get-child: func() -> container-child;
  }

  variant container-child {
    foo(foo-data),
    unmodeled(string),
  }

record container-input {
    child-event-type: container-child-types,
    data: string
  }

enum container-child-types {
    foo,
    unmodeled,
  }

Last updated: Dec 23 2024 at 12:05 UTC