Stream: general

Topic: How to use variant types or similar in C#?


view this post on Zulip godotdot (Feb 09 2025 at 17:10):

Hey all,
I'm looking at using componentize-dotnet and am having trouble understanding how to use types such as variant, option or result in C#. I'm not sure what I need to implement or what, if anything, is generated for me to use or how to use it.

Are there any examples of how this should be done?

view this post on Zulip godotdot (Feb 11 2025 at 00:37):

Ok, I'm sure I'm just doing something wrong but at this point I'm tempted to say result doesn't work properly with C#.

So far as I can see, if a function is defined in wit to return a result, the C# code is incorrectly expected to return the type the result is parameterized by; so if the wit defines the function as returning result<int, string>, the C# code expects int, not result<int, string>.

view this post on Zulip godotdot (Feb 11 2025 at 02:14):

All right, if I understand correctly, if you're implementing an exported function that returns a result, there are generated bindings that call the function you implement, and it is they that return Ok or Err based on whether your implementation throws an exception.

So the behavior I described before, where you just need to return the value that would get wrapped in an Ok, makes sense if you know that.

I'd have to think about it more, but I feel like I'd rather handle returning the Ok/Err myself.
As it is, the function I have to implement in this case has a misleading signature that doesn't match the wit.
Also, it has to throw an exception for Err, and my understanding is exceptions are not great for performance.

view this post on Zulip Joel Dice (Feb 11 2025 at 02:52):

You may be interested in this PR and the option it added.

view this post on Zulip godotdot (Feb 11 2025 at 03:46):

That does look interesting, thank you!
I'm not sure how I should use it, though. I was thinking maybe adding <Wit Update="gen-host.wit" World="gen-host" feature="with_wit_results=false" /> to the .csproj might do the trick, but it doesn't seem t.

view this post on Zulip Joel Dice (Feb 11 2025 at 13:11):

@James Sturtevant do you know the answer to this?

view this post on Zulip James Sturtevant (Feb 11 2025 at 16:10):

I don't think we pipe through extra setting to witbindgen yet. @godotdot Could you open an issue?

view this post on Zulip godotdot (Feb 11 2025 at 17:27):

Consider it done @James Sturtevant https://github.com/bytecodealliance/componentize-dotnet/issues/66

The option to generate Result types instead of handling Results via Exceptions has been implemented in bytecodealliance/wit-bindgen#1115, but doesn't appear to be getting used.

view this post on Zulip James Sturtevant (Feb 11 2025 at 20:57):

Thanks! I am AFK for a couple days but will take a look soon

view this post on Zulip godotdot (Feb 11 2025 at 21:50):

Cool, thank you!

view this post on Zulip James Sturtevant (Mar 08 2025 at 01:24):

@godotdot this should be resolved in https://www.nuget.org/packages/ByteCodeAlliance.Componentize.DotNet.Wasm.SDK v0.6.0.

docs: https://github.com/bytecodealliance/componentize-dotnet?tab=readme-ov-file#passing-additional-wit-bindgen-args

Tooling for creating WebAssembly components from C#
Tooling for creating WebAssembly components from C# - bytecodealliance/componentize-dotnet

Last updated: Apr 08 2025 at 20:03 UTC