My recent project needs fixed point float, it would be nice to have AngelScript support custom literal, e.g. 3.14f32
to define fixed32 literal constant. I've noticed that some people already suggested this feature in To-Do list a decade ago.
I'm investigating the compiler of AngelScript and trying to add something like user-defined literals of C++ (operator""_suffix
).
My currently planned solution is to add new behaviors asBEHAVE_LITERAL_CONSTRUCT/FACTORY
and use the name of behavior as the suffix, e.g. RegisterObjectBehaviour("void f32(double)")
for f32
suffix.
The parameters of special functions can be one of the following (with example usage)
uint64
:void h(uint64)
for1h
for convenient date time interfacedouble
:void i(double)
for imaginary number.const string&in
for constructing a literal from string. It requires the host to register string support at first.int&in, uint
, the C++ side will beconst char*, asUINT
. It will directly pass the source code of literal in script for parser provided by user. This might need the engine to expose some utilities such as tools for parsing script string literal.
For example, maybe we can implement a fmt
suffix for formatting after this feature is done.
int val = 42;
string str = "hello";
string result = "val = {val}, str = {hello}"fmt;
// result is "val = 42, str = hello"
Please give some suggestion on this solution.
For reference: User-defined literals (since C++11) - cppreference.com