-
Notifications
You must be signed in to change notification settings - Fork 371
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 are ugly #952
Comments
As of 016557d, there's a
|
I still think the reader macro looks a tad uglier... |
Remember decorators can also apply to classes. I think I like the reader macro better. |
Lots of things are valid lisp, yet, ugly. I'd have a counter-proposal:
I'd put the decorators after the function name, and use a (These are just random ideas, I have no preference whatsoever, as I am fine with |
Having decorators declared after the function name makes more sense to me. It makes it easier to spot function name in a glance when reading code and I would argue that the function name is more important than decorators being applied to the function. We have to remember to consider the case where function is being decorated with more than one decorators.
@gilch has a good point about being able to decorate classes. It would be nice to have the same syntax on both function and class definition, so one has to remember less of syntax.
|
Still not a syntax fan, but it'd probably be pointlessly stupid to argue further, since my suggested syntax would require more special-casing. Closing! |
I'm starting to reconsider this one now that I've started using the
We would also need to do that for classes.
Think about how lines will change in version control if you have to wrap the whole function. It will mean re-indenting the whole thing when you just added a decorator on top. Python decorators don't have this problem. You could also spread the list over multiple lines to have a good way of changing and re-ordering decorators with version control.
|
Another reason to change it is for code folding. If you fold everything in your editor, you'd really want the first line of the top level form to have the class or function name. Look what the current decorator syntax does to an excerpt of #1328 when folded:
We can't tell them apart anymore. With the proposed syntax, the first one folds like
You could even add multiple decorators on the same line, like
It's an improvement, but once you take multiple lines, the name disappears when folded.
So perhaps the function name should still come first. But then where do the decorators go? If you put it right after the name, it looks like the arguments list. And a function needs to be able to return a list, so it probably shouldn't go next (even though we do that with docstrings, because Python). So how about putting it first and using
When folded and the decorators list is spread over multiple lines, you'd still lose sight of the arguments list--
--but decorators can change that anyway, so they're more important to put first. Of course, this is optional. When there's no Classes would have to work the same way.
|
You can also just write |
@Kodiologist the hole left is how to do class decoration though. |
Hy has no equivalent to |
Yeah, I realize, but do you really want that to be the official suggestion? Though you do have a point... it wouldn't be too hard to provide a Not sure how PEP 487 would fit in, but its an idea. |
Or you can define the class as normal and then afterwards An anonymous class doesn't really make sense, since you often need to refer to its name in methods and for inheritance. Even I wonder if the class form could return itself instead of When interfacing with Python libraries, decorators and metaclasses could come up a lot. It probably shouldn't have any more ceremony than Python does. I don't really have a better idea than adding them to the function and class forms. I'm reluctant to make the special forms any more complex than they already are because that complicates |
I was musing out loud about the later (not really anonymous but more like expressions), and yeah, its not great.
We can revisit this again when #1482 lands. I did start on it, just got stuck needing to fix the importing system (specifically just need self imports to work). (defn addition
{:decorate inc-decorator}
[a b]
(+ a b)) Isn't too bad... |
Thinking aloud again.
Decorators are either symbols or HyExpressions. The list is terminated with the arguments HyList. A macro can tell the difference. This re-orders the same way as
Maybe you just indent it differently as a matter of standard style:
That looks more like Python, but doesn't help with the folding. Maybe you could just wrap the name to make it look like a call.
That's pretty explicit (reminds me of scheme functions) I feel like rewriting it to use Maybe we could use a
Note that the body indent need not be changed now, even if the decorators are removed. This makes it play more nicely with version control. It also looks more like the Python.
It's also a bit easier to add decorators this way in the editor, since you don't have to wrap more parentheses. Just insert |
Metadata could work, I suppose. Macros can see it after all. But what if you want to attach more than one? The above syntax doesn't quite work. You'd have to wrap it in a list or something. |
I ended up making decorator syntax part of the Although Hebigo's macros are optimized for Hebigo's constraints, which don't always look optimal in S-expressions, I'm pretty happy with how this one looks, even when used in Lissp. |
is ugly. My idea is to add something like:
Thoughts?
The text was updated successfully, but these errors were encountered: