pigpigyyy / Yuescript

A Moonscript dialect compiles to Lua.
http://yuescript.org
MIT License
424 stars 35 forks source link

[feature request] String interpolation for logging #142

Open SkyyySi opened 1 year ago

SkyyySi commented 1 year ago

In Python, there's a useful little feature that allows writing something like the following:

foo = "bar"
print(f"{foo = }")

which prints:

foo = 'bar'

Notice how it preserved the spaces before and after the equals sign as written in the f-string.

I think this feature would be a very nice addition to Yuescript, although it may need some simplification. In Python, the code actually translates to printing the repr of foo, not the string conversion; an equivalent would be to write print("foo = " + repr(foo)). But of course that doesn't exist in Lua. My suggestion would be to either just print the value's tostring(), or maybe using a library function to pretty print it. While Yuescript seems to generally prefer inlining things to prevent being dependent on any libraries, I believe it would be fine here, because this is a pure development/debugging feature, so it's pretty safe to expect someone to have Yuescript installed if their code makes use of it.

pigpigyyy commented 1 year ago

I'm currently using a pretty print function taken from Moonscript's library. You can use it as:

import "yue" as :p
foo = "bar"
tb = {1, 2,3, abd: 123}
p foo
p tb

So you suggest that we can make some pretty print function code inlined, so that we can do it without importing extra libs?

SkyyySi commented 1 year ago

So you suggest that we can make some pretty print function code inlined, so that we can do it without importing extra libs?

No that's not what I meant. I talked about pretty printing a little too much at the end of my post so I probably should have made it more clear that that wasn't the focus. Instead, the important take away was actually the logging syntax for string interpolation. I didn't know that Yuescript already has a pretty printing function in its library, so I didn't mention it originally.

The thing I was talking about would be this:

foo = "bar"
print "#{foo = }"

which would compile to something like this:

_pretty_0 = require("yue").p
local foo = "bar"
print("foo = " .. _pretty_0(foo))