what’s the most straightforward way of creating a function decorator to do logging of function inputs/outputs? I’m thinking there has to be something like advice in emacs-lisp or decorators in python/js I could leverage, but google is failing me
you could try trace
ah, I guess that’s nice. Not quite what I’ looking for here but good to know about definitely
Alright, one more question - So in my program I find I am using a bunch of requires regularly, threading, racket/matrix, racket/array, data/collections
Is it idiomatic for me to define my own language that extends racket or racket/base or something but already has those requires in it?
I’m not sure there’s anything idiomatic like that, but that’s not something frowned upon at least, afaict
I think there’s even a package/utility that helps with the repetition of provide/require, but I don’t recall exactly what
For the sake of DRY, it should even be encourage I guess
You don’t need a new language. You can make a module in, say, “foo.rkt”.
(require threading racket/matrix racket/array data/collections)
(provide (all-from-out threading racket/matrix racket/array data/collections))
And simple use (require "foo.rkt")
in your files.
Not quite, how? (I ask because racket/trace
is like advice or decorators. IIRC trace
can mutate an already existing definition (which is like advice IIUC) and define-trace
does it as part of defining the definition (which is like decorators IIUC).)
p.s. Some months ago I was exploring some ideas around tracing/logging with https://github.com/greghendershott/vestige. It’s not a package, it’s just a repo; you’ll have to read the docs as .scrbl not html; and I don’t know if/when I’ll make it “more official”. But you’re welcome to take a look on the chance it’s of any interest or use.
on my read of the docs it will output on every function call, and it also requires starting racket a certain way (-l trace
?) So maybe I’m missing something but for scripts which call lots of functions (usually via recursion or in loops) or where you can’t easily control how racket
is run (literate programming) it’s not as easy as just (around-advice ...)
or somtehing.
But again, I might be misreading hte docs
hmm fair enough
Some time ago I wrote a tracing capability for my own use, which allows me to trace only the functions I want. Basically, you can replace (define (some-function ...) ...)
with (define/trace (some-function ...) ...)
to trace a function. I suppose it is somewhat like decorators in other languages… If you are interested, you can find it here: https://gist.github.com/alex-hhh/331eb722ca7375ea6b53169db847d921
@gmauer Maybe you’re thinking of errortrace
(one reason I named my thing “vestige” is because “trace” is so overloaded :smile:). Anyway you just (require racket/trace)
. Then you can either replace define
with trace-define
(which I suppose is ~= a decorated definition) or use the mutating trace
(which I supposed is ~= advising an already-defined function).
The mutating trace
and untrace
are handy if you’re in a REPL, you can toggle the tracing on and off.
ah, yeah ok, I see that - I was looking at this page and didn’t see that https://docs.racket-lang.org/trace/index.html#%28mod-path._trace%29