anoma / juvix

A language for intent-centric and declarative decentralised applications
https://docs.juvix.org
GNU General Public License v3.0
449 stars 54 forks source link

Add support for unsigned 8-bit integer type Byte #2918

Closed paulcadman closed 2 months ago

paulcadman commented 2 months ago

This PR adds Byte as a builtin with builtin functions for equality, byte-from-nat and byte-to-nat. The standard library is updated to include this definition with instances for FromNatural, Show and Eq traits.

The FromNatural trait means that you can assign Byte values using non-negative numeric literals.

You can use byte literals in jvc files by adding the u8 suffix to a numeric value. For example, 1u8 represents a byte literal.

Arithmetic is not supported as the intention is for this type to be used to construct ByteArrays of data where isn't not appropriate to modify using arithmetic operations. We may add a separate UInt8 type in the future which supports arithmetic.

The Byte is supported in the native, rust and Anoma backend. Byte is not supported in the Cairo backend because byte-from-nat cannot be defined.

The primitive builtin ops for Byte are called OpUInt8ToInt and OpUInt8FromInt, named because these ops work on integers and in future we may reuse these for a separate unsigned 8-bit integer type that supports arithmetic.

Part of: