cashscript icon indicating copy to clipboard operation
cashscript copied to clipboard

separate type casting and padding into separate behavior

Open mr-zwets opened this issue 7 months ago • 2 comments

the current situation of when padding occurs is not very consistent and requires a confusing workaround to cast a bytes item to a certain size

// from int to bytes
bytes(int) -> typecast
bytes6(int) -> padding
bytes(int, 6) -> padding

// from bytes to bytes
bytes6(bytes) -> typecast
bytes6(int(bytes)) -> padding
bytes(int(bytes), 6) -> padding

I suggest a new function toBytesX(item, byteLength) which takes care of the padding

So the new way would be

// from int to bytes
bytes(int) -> typecast
toBytesX(int, 6) -> padding

// from bytes to bytes
bytes6(bytes) -> typecast
toBytesX(bytes, 6) -> padding

possible alternative syntaxes toBytesLength, padBytes, or resizeBytes.

what would break

The second argument in bytes(item, length) would simply be deprecated, this can be done over time, first with a warning.

bytes6(int) would simply be disallowed and result in a type error at compile time, only bytes type would be able to cast to a bounded bytes type

mr-zwets avatar Mar 27 '25 16:03 mr-zwets