-
Notifications
You must be signed in to change notification settings - Fork 82
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
feat(builtin): add dump()
for easy print-based debugging
#1269
base: main
Are you sure you want to change the base?
Conversation
@@ -26,6 +29,18 @@ pub fn print[T : Show](input : T) -> Unit { | |||
println(input) | |||
} | |||
|
|||
///| | |||
/// Prints and returns the value of a given expression for quick and dirty debugging. | |||
/// @alert deprecated "This function is for debugging only and should not be used in production" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is marked as deprecated because it seems to be the only way to generate warnings with it. In Rust this logic is handled by clippy
instead of rustc
so there haven't been such concerns.
Pull Request Test Coverage Report for Build 4053Details
💛 - Coveralls |
println("dump(\{name}@\{loc}) = \{any_to_string(t)}") | ||
t | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any_to_string is introduced for arbitrary code transformations where T
is not sure if it satsifies Show
. How is this different from dump[T:Show](...)
cc @Guest0x0 it makes sense to introduce support of ArgsRepr which is essentialy a stringified repr of args. so that we can do something like this:
pub fn dump[T:Show](t : T, name~ : ArgsRepr = _, loc~ : SourceLoc = _) -> T
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bobzhang One thing that distinguishes Swift's dump()
from Rust's dbg!()
is that the former doesn't pose any restrictions on T
whereas the latter depends on T: Debug
(which is exclusively used for print-oriented pretty-printing, compensating for the lack of a built-in dumper in Rust) 1.
I think the Show
trait in MoonBit corresponds to the Display
trait (which subsumes .to_string()
in Rust) rather than Debug
, so it makes more sense to add no bounds at all.
OTOH ArgsRepr
will definitely be a nice addition.
Footnotes
-
The lack of distinction between
Display
andDebug
seems like a common reason for which print-based debugging is less popular in traditional tech stacks; both approaches here allow the mitigation of this issue. ↩
dbg!()
is a very popular Ruststd
macro that can be used like so:... note that
a * 2
is not only printed for debugging but also passed on to the rest of the calculation, which makes it a perfect fit for inspecting complex evaluations such as those of chained method calls.The idea of this PR is to mimic the behavior of that macro with what is currently available with MoonBit's compiler magic, i.e.
loc~ : SourceLoc = _
and"%any.to_string"
.The signature of this function
dump()
is as follows:... where the identifiers
dump
andname
are taken from a similar Swift function.