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

use Zig idioms #605

Open
garrisonhh opened this issue Jan 15, 2023 · 3 comments
Open

use Zig idioms #605

garrisonhh opened this issue Jan 15, 2023 · 3 comments

Comments

@garrisonhh
Copy link

From the perspective of someone familiar with Zig features, the wasm4 examples provide code that looks like it was directly translated from C rather than written with Zig's stdlib and features in mind.

std.PackedIntSlice

To use DRAW_COLORS as an example, instead of using a *u16 to represent what is actually 4 fields, you can use std.PackedIntSlice:

// status quo
pub const DRAW_COLORS: *u16 = @intToPtr(*u16, 0x14);
// idiomatic representation
pub var DRAW_COLORS = std.PackedIntSlice(u4).init(@intToPtr(*[2]u8, 0x14), 4);

Framebuffer could be similarly improved using std.PackedIntSlice.

This REALLY simplifies things like the pixel() example in the basic drawing docs.

int-backed enums

All of the prefixed constants could be represented much more nicely with enum(u8) or enum(u32). To use buttons as an example:

// status quo
pub const BUTTON_1: u8 = 1;
pub const BUTTON_2: u8 = 2;
pub const BUTTON_LEFT: u8 = 16;
pub const BUTTON_RIGHT: u8 = 32;
pub const BUTTON_UP: u8 = 64;
pub const BUTTON_DOWN: u8 = 128

// idiomatic representation
pub const Button = enum(u8)
    .@"1" = 1 << 0,
    .@"2" = 1 << 1,
    .left = 1 << 4,
    .right = 1 << 5,
    .up = 1 << 6,
    .down = 1 << 7,
};

slices

While trace wraps traceUtf8 nicely, the same idiom is not extended to diskr and diskw.

tracef

std.fmt and writers are to zig as printf and FILE pointers are to c. Implementing a buffered writer wrapper over trace would be simple, idiomatic, and allow zig users a lot of convenience when interacting with the stdlib.

Conclusion

I am happy to implement all of these features and submit a PR, as a new user of wasm4 and a first-time contributor I figured the responsible thing would be to post an issue for discussion first!

@garrisonhh garrisonhh changed the title Zig implementation uses obtuse types use Zig idioms Jan 15, 2023
@desttinghim
Copy link
Contributor

Having used zig to develop for WASM4, one thing to keep in mind for the tracef example is size. The code generated for std.fmt can end up being quite large (for WASM4 anyway). For debug this should be fine though.

I mostly wanted to chime in and say this was a good idea! Good luck with the PR 🙂

@aduros
Copy link
Owner

aduros commented Feb 21, 2023

In my opinion there are a couple important benefits to keeping the default bindings boring and unidiomatic.

  1. It makes it easier for users already familiar with one WASM-4 language to approach learning a new one.
  2. It avoids a lot of unnecessary bike shedding 🙂

Obviously this is kinda subjective, and we've made exceptions in the past (as in Odin where the author of the language contributed the bindings). In general though, I think I'd rather see users writing and distributing their own bindings that use advanced syntax. There are a couple different Rust crates for WASM-4 for example.

@wooster0
Copy link
Contributor

I think one thing we could do though is lowercase the constants. So, w4.screen_size instead of w4.SCREEN_SIZE as writing constants in uppercase like this is absolutely unnecessary in Zig, and unidiomatic.

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

No branches or pull requests

4 participants