Hacker News new | ask | show | jobs
by KallDrexx 433 days ago
Author here, yes I am on an insane quest to get C# to run anywhere and SNES was just a random side tangent to that :D.

So the code in the game example is very much a "port directly from the example C code to make sure it's possible and it works". The next step was to start looking at where things can be made more idiomatic.

For example, C# supports static methods in interfaces now. This means instead of passing function pointers around for things you can create a class that implements an interface and call `InitObjects<T>()` instead of `InitObjects(&initFunction, &updateFunction, null)`, and I think I can do that in a no overhead way.

There are some other ideas I have for making things more idiomatic, but I really wanted to make sure I had a working ROM first before I started mangling the examples and trying to figure out if it's my transpiler or my logic that's breaking things.

There is a limit to how idiomatic I can make it though. Since the SNES has limited memory, any stack variable you create risks accidentally overwriting other memory. So it seems like you want to limit how many stack variables you use, which ends up meaning storing things in pointers and globals when using them.

There are some other slight advantages, like better enums, but yeah. At the end of the day this is a usecase of my transpiler allowing C# to be used on non-standard platforms.

1 comments

> I think I can do that in a no overhead way.

If 'T' is a struct, yup, although that's specific to the way it is compiled by CoreCLR's JIT or ILC.

For constructor semantics it's best to expose a static abstract interface method Create<T> and have the interface be SomeInterface<Self> where Self : ISomeInterface<Self>.

For example, that's how `INumber<T>` interface works so you can further down the line write `T.Zero`.

Yep, in the current restrictions `T` has to be a struct (no heap allocations means no reference types are supported).

Huh, interesting idea with the `Self` generic dependency for the `Create<T>`. That's an interesting angle I hadn't thought of, thanks.