-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature request] String interpolation for logging #142
Comments
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? |
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)) |
@pigpigyyy Hey, I was just wondering, is this still something that's considered to be added? Even if it were to be the most basic form (like below), I would personally still find it immensely useful since I use print-debugging... way more than I probably should 😅 print "#{expression = }" print("expression = " .. tostring(expression)) |
How about using a macro for better logging? macro d = (...) ->
import "yue" as options: :release
unless release
"print " .. table.concat ["\"#{v} = \" .. tostring(#{v})" for v in *{...}], ","
else
""
tb =
a: 123
b: "abc"
val = 998
s = "xyz"
$d tb, val, s
nil when builds with: > yue test.yue gets: local tb = {
a = 123,
b = "abc"
}
local val = 998
local s = "xyz"
do
print("tb = " .. tostring(tb), "val = " .. tostring(val), "s = " .. tostring(s))
end
return nil builds with: > yue --release test.yue gets: local tb = {
a = 123,
b = "abc"
}
local val = 998
local s = "xyz"
return nil |
In Python, there's a useful little feature that allows writing something like the following:
which prints:
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 writeprint("foo = " + repr(foo))
. But of course that doesn't exist in Lua. My suggestion would be to either just print the value'stostring()
, 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.The text was updated successfully, but these errors were encountered: