neverwinter.nim icon indicating copy to clipboard operation
neverwinter.nim copied to clipboard

scriptcomp: Unary stringification operator `$`

Open mtijanic opened this issue 1 year ago • 0 comments

Unary operator (like ~ or !) defined for all stock types as producing a string. e.g.:

int n = 3;
string s1 = $n; // s == "3"
vector v = Vector(1.0, 2.0, 3.0);
string s2 = $v; // s == "{1.0, 2.0, 3.0}"
string s = "hello";
string s3 = $s; // s3 == "hello"

This would allow you to print a variable as string regardless of its actual input type, which allows for better code reuse, especially if coupled with a macro system of sorts.

As a stretch goal, it could support custom types too, as:

struct MyStruct { int n; };
string $_MyStruct(struct MyStruct x) { return "MyStruct: { n = " + $x.n + "}"; }

Would need VM changes, but the bulk is compiler-side

mtijanic avatar Jul 07 '23 12:07 mtijanic