Skip to content

Use BytesView instead of Bytes (+add Utf8 hint type)#50

Merged
orsinium merged 1 commit intomainfrom
feature/bytesview
Feb 28, 2026
Merged

Use BytesView instead of Bytes (+add Utf8 hint type)#50
orsinium merged 1 commit intomainfrom
feature/bytesview

Conversation

@applejag
Copy link
Collaborator

Changes to use BytesView more, as it seems to be a better convention for function parameters as Bytes implicitly converts to a BytesView.

I also added "hint alias types" for Utf8 and Utf8View to try better hint where UTF-8 input is required. Sadly the LSP currently only view the aliased type Bytes/BytesView, and doesn't even allow "jump to definition" for type aliases. And there's currently no way to get string literals to implicitly convert to custom types. Bummer. But maybe that will be fixed in the future?

One issue here is that using views will force import the stdlib's abort implementation, which does some string and SourceLoc shenaginans which brings in ~5kB of code: https://github.com/moonbitlang/core/blob/05aaf6a8164093e22247519255cccdcabd7870c6/builtin/intrinsics.mbt#L64-L78

No additional code is imported if the app already uses any other standard library function that calls @builtin.abort, which for more complicated projects is somewhat unavoidable.

@orsinium
Copy link
Member

And there's currently no way to get string literals to implicitly convert to custom types.

Not good :( At least if I get it right. Most of the strings are static strings. Also, returning (for instance, from get_name) Bytes is better than BytesView since Bytes is more specific. "Be conservative in what you return".

@applejag
Copy link
Collaborator Author

To be clear, I meant "no way to get string literals on custom structs"

You do get the implicit conversion on type aliases, like we have in this PR.

However in this snippet:

@firefly.log_debug(("random float: " : Utf8) + format(@random.float()))
@firefly.log_debug(("random double: " : Utf8) + format(@random.double()))
@firefly.log_debug(("random int: " : Utf8) + format(@random.int()))
@firefly.log_debug(("random uint: " : Utf8) + format(@random.uint()))
@firefly.log_debug(("random int64: " : Utf8) + format(@random.int64()))
@firefly.log_debug(("random uint64: " : Utf8) + format(@random.uint64()))

^I had to add the ( ... : Utf8) because when the parameter is a BytesView then the string literal was interpreted as a string when doing +

@orsinium
Copy link
Member

Alright, I trust you. You're the MoonBit expert here :)

@orsinium orsinium merged commit 2c8f334 into main Feb 28, 2026
2 checks passed
@orsinium orsinium deleted the feature/bytesview branch February 28, 2026 20:22
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

Successfully merging this pull request may close these issues.

2 participants