Skip to content

Add raw bytes from string

type:added

For LIGO developers

Old discussion from previous proposal

This changes the semantics of string literals for type bytes.

Currently we have two kinds of bytes literals:

  1. 0x666f6f
  2. ("666f6f" : bytes)

I don't know whether anyone has ever used the latter form. It does not seem to be used in any Ligo tests, anyway.

This MR changes it so that the string is interpreted directly as bytes, rather than hex-decoding. This is useful because bytes are conventionally used for strings which might contain characters falling outside of Michelson's restrictive regime -- for example for contract metadata. With this change we can write them in readable form in Ligo files:

("foo" : bytes) = 0x666f6f

However, this changes the semantics of the old literals, now we have:

("666f6f" : bytes) = 0x363636663666

Is this OK? I don't know.

Perhaps instead we could invent a pretend operator:

Bytes.of_string "foo"

but this seems a little strange: it needs to be compiled to a bytes literal (so e.g. Bytes.of_string e must fail in general unless e is or is optimized to a literal string) and it breaks with our usual "literals of different types via ascription" practice (which admittedly is also a little strange.)

New proposal

In the end, a third option is explored and implemented: using [%bytes "..."] to write bytes from strings. The benefit is that this change is not breaking, and it is not as strange as pretending an operator Bytes.of_string.

Changelog details:

Raw bytes representation from string

Raw bytes can be expressed directly from a string using [%bytes "..."]. E.g., [%bytes "foo"] represents bytes 0x666f6f.

Edited by E. Rivas

Merge request reports

Loading