-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Decorators support for defasync
and anonymous functions
#1178
Comments
By my reading,
I am skeptical about this argument personally, especially since it's being made for asynchronous decorators which are fraught even for experienced developers. That being said, I would accept a patch to move the support for |
I believe so, yes.
Sure, I will look into it, thanks |
Hi @chrisrink10 , I looked into the (def ^:macro ^:redef fn
(fn* fn [&env &form & decl]
(with-meta
(cons 'fn* decl)
(meta &form))))
;; ...
;; inside the `defn` macro
... `(def ~fname ~fn-body)))) However, they seem to share a common path though in I'd also like to add that after the fix for #1187, the Based on the latest findings, I see the following options moving forward
I think ideally the order of preference should be reversed from the list above: option 4 will be the most ideal, followed by option 3 and so on. Practically, though, it seems better to start with option 1 and see how far we can go up the list? What do you think? Thanks |
ah right, I just realised that the decoration handling code in fn-body (if decorators
(loop [wrappers (seq (python/reversed decorators))
final `(fn ~fname ~@body)]
(if (seq wrappers)
(recur (rest wrappers)
`(~(first wrappers) ~final))
final))
`(fn ~fname ~@body)) So most probably what you meant is to pass in the fn-body (if decorators
`(fn ^{:decorators ~decorators} ~fname ~@body)
`(fn ~fname ~@body)) |
The metadata should already be being passed into You'll probably want to inspect both the name and the |
Hi, could you please review patch to support the `:decorators` key in anonymous `fn`s. It addresses #1178. The handling of `:decorators` has been nmoved to `fn`, which also ensures that the meta key is now considered when passed as metadata to the `defn` name. This enables support for supporting the meta key in `defn` derived macros such as `defasync`, as intended. I've updated the `defn` docstring to hint that the processing is now done by `fn` now. I've also added tests for `fn`, `defn` and `defasync`, along with a section in the python interop doc about it. Thanks --------- Co-authored-by: ikappaki <ikappaki@users.noreply.github.com> Co-authored-by: Chris Rink <chrisrink10@users.noreply.github.com>
Fixed in #1189 |
Hi,
Currently, there doesn't seem to be a way to decorate a function defined with
defasync
.The
defn
macro supports a:decorations
attr-map?
key, but whiledefasync
is built on top ofdefn
, it only considers the:decorations
key in theattr-map?
and not in metadata.As a result, passing a metadata
:decorator
key, such as in(defasync ^{:decorators [decorator]} something [] ...)
, has no effect.Would it be advisable to also support a
^{:decorators [...]}
meta key fordefasync
?Furthermore, when adapting examples from Python to Basilisp, developers often encounter decorated functions without necessarily understanding how they are internally defined.
For anonymous functions, decorating them is not immediately intuitive. A developer with basic knowledge of decorators might deduce that the anonymous function can be passed as the first argument to the decorator, e.g.,
(decorator (fn [] ...))
. However, if the decorator accepts arguments, it must first be instantiated with those arguments before the anonymous function is passed, e.g.,((decorator arg1 arg2) (fn [] ...))
.Given that this requirement assumes Basilisp developers would need to understand some internal details about decorator (something that is not necessarily expected when using Basilisp, especially when coming from a Clojure background) would it be better to extend support for the
:decorators
meta key to anonymous functions as well? This enhancement could be particularly valuable when transpiling Python code to Basilisp, making it more intuitive and easier to visually pair code between the two languages. Alternatively, we can just extend the documentation to indicate how to manually decorate anonymous fns.With that in mind, my suggestions would look something like this:
I have conducted a brief survey below to show the current status. I've decided to create the decorator in python to ensure I don't overlook anything:
asyncdec.py
run:
And then I imported the python
atimer
decorator for the surveyissue.lpy
run:
The text was updated successfully, but these errors were encountered: