savi-lang / savi

A fast language for programmers who are passionate about their craft.
BSD 3-Clause "New" or "Revised" License
156 stars 12 forks source link

Add `Integer.format` method with decimal and hexadecimal options. #287

Closed jemc closed 2 years ago

jemc commented 2 years ago

This sets up the new standard convention for formatting values in variable ways, compatible with string interpolation.

The new convention is to use a "struct DSL" initiated with a format method, with various DSL methods chained onto it which may create successive structs after that using values from earlier ones in the chain.

For example, "example: \(99.format.hex.with_prefix("\\x")" will render as "example: \x63" after interpolation.

Using named methods instead of terse format specifier symbols (a la printf) fits with Savi's goal of being imminently readable.

Using structs (as opposed to classes) for the DSL avoids heap allocation (as long as the struct doesn't get subsumed into an abstract type), which fits with Savi's goal of being very performant.

Any user-defined type can make use of this same convention for formatting (rather than it being restricted to only certain pre-known formattable types) which fits with Savi's goal of being simultaneously consistent and open-ended.