Skip to content
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

Open
SkyyySi opened this issue Jul 1, 2023 · 2 comments
Open

[feature request] String interpolation for logging #142

SkyyySi opened this issue Jul 1, 2023 · 2 comments

Comments

@SkyyySi
Copy link

SkyyySi commented Jul 1, 2023

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
Copy link
Owner

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
Copy link
Author

SkyyySi commented Jul 2, 2023

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants