Solod – A Subset of Go That Translates to C

https://github.com/solod-dev/solod

GitHub - solod-dev/solod: A subset of Go that translates to C

A subset of Go that translates to C. Contribute to solod-dev/solod development by creating an account on GitHub.

GitHub

I was curious how defer is implemented. `defer` in Go is famously function-scoped, not lexically-scoped. This means that the number of actively-deferred statements is unbounded, which implies heap allocation.

The answer is that Solod breaks with Go semantics here: it just makes defer block-scoped (and unavailable in for/if blocks, which I don't quite get).

https://github.com/solod-dev/solod/blob/main/doc/spec.md#def...

solod/doc/spec.md at main · solod-dev/solod

A subset of Go that translates to C. Contribute to solod-dev/solod development by creating an account on GitHub.

GitHub

What's the point if it's incompatible? The README suggests using go's testing toolchain and type checker, but that's unreliable if the compiled code has different behavior than the tested code. That's like testing and typechecking your code in a C++ compiler but then for production you run it through a C compiler.

Would have been a lot more useful if it tried to match the Go behavior and threw a compiler error if it couldn't, e.g. when you defer in a loop.

Is this just for people who prefer Go syntax over C syntax?

The implementation does exactly as you suggest: it throws a compiler error if the defer is not top-level (is. inside of a loop or condition). That way the only accepted constructs behave the same regardless of whether defer is implemented as block-scoped or function-scope).