I’m not sure where this feature suggestion should live, or who to send a feature request to — any ideas please let me know!
It’s not something I’d be able to build myself, but think it could be very handy when trying to understand how a function works (or, indeed, a program) …
I seem to remember a Mario animation that logged it’s progress … but I’m not sure where that package is.
Elm’s current roadmap doesn’t include this (any?) kind of feature. I think Gren (another language that descends directly from Elm) recently implemented step by step debugging, so maybe your suggestion would be more appreciated there.
@benjamin-thomas Oh yeah that example is helpful. Are there any good guides you can point me to for tutorials on how to properly use Debug.log? I’m not entirely sure where to place it in the program (I’ve tried to use it in case branch -> which doesn’t work)
@Maldus512 I’ve taken a brief look at both Gren and Roc lang — Roc looks promising, but Gren seems quite a small community. Worth a shot to mention the feature I suppose.
You can use it anywhere you can call a function. In a case branch, you can only use special pattern matching syntax (as I mentioned in the other thread). It’s not possible to call functions there. I’m not sure if it helps, but if you are familiar with the identity function: Debug.log is the identity function, with the side effect of logging its argument to the console.
I think code can get quite messy fast using that function, so might be better used sparingly, checking each function as you go. And I suppose unit tests will go a long way to keeping bugs at bay.
Note that in Elm circles, “debugger” means the time travelling debugger UI that you get by compiling with the --debug flag. What I used in a let is the Debug.log function.
While the let _ = Debug.log "message" thing in “trick” is handy many times, it’s also really handy to use Debug.log in existing expressions. For example, if you have this:
numbers
|> List.reverse
|> List.map square
You can add some Debug.log to see the result after each operation:
Debug.logs are added temporarily where needed to debug a problem, and are then removed again. When compiling an Elm app with the --optimize flag, using the Debug module is not allowed. There is also an elm-review rule to help you remove Debug.log calls you left behind after debugging: NoDebug.
@miniBill Sounds very interesting; I think I’d need a bit more documentation to understand how it works though — I’m a beginner with Elm. Is it better for smaller expressions, or can it handle small programs too?
It can currently handle simple expression or single modules. If given a module it will try to evaluate the function called main, regardless of type.
It implements the whole of elm/core and no other library.
To use it you need to clone the repository, then run make to prepare all the codegenned files. Once you’ve done that you can run yarn elm-watch hot, and connect to the address it will print out.
The UI has an input but for you expression or module, and you want the Trace button to execute the code while keeping track of values.