-
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
Get rid of the __init__ return suppression automagic and replace with a definit macro #1544
Comments
I share your discomfort with |
I did start working on #1482, musing outloud, I wonder if a metadata attribute could help here: (defmacro definit [argslist &rest body]
`(defn ^:noreturn __init__ ~argslist ~@body)) |
@vodik I like your thinking, but that's even harder to remember and longer to type the In almost all the cases when you'd want to suppress the auto return, you could explicitly return |
@Kodiologist I think putting it first instead of last helps a lot, mentally, especially if it's easy to form a habit. Programmers have limited working memory. You have to think hierarchically and do small pieces at a time. The hardest part of writing a method, in terms of cognitive load, is usually the body. When you're thinking at the "method body" level, you're not thinking at the "class outline" level anymore. Remembering to add But if you've got a better idea for how to do this without automagic, I'd like to hear it. I do think the explicit |
I don't have any better ideas, no. Although, I guess it's not a big problem if you forget the |
Just use an explicit The existence of an early
I'm still uncomfortable with the automagic for the other reasons though, like decorators and metaclasses. Look at this:
compiles to
The return wasn't suppressed. Is that expected? Good? Bad? I don't even know. Automagic is too confusing. |
Yeah, I guess it's just not that bad even without any solution. It's easy to find and fix. It's just annoying when you forget. I do think it's worth a macro (because of the aforementioned cognitive load), but I'd approve just removing the automagic then. Another option might be a
I really don't like this. There should be one-- and preferably only one --obvious way to do it. I'm just throwing that out there in case it inspires a better idea. |
Just a note, I was hoping to bring |
I don't know if this is a big problem to worry about. 90% of Maybe the rewrite can be dropped any nobody will even notice 😉 |
That was my original plan, and I still support it. @Kodiologist suggested renaming
It didn't always. Maybe we wouldn't have bothered with the automagic if it had. But I'm not sure if that makes it better, since people will forget they always have to
You can configure self in |
Guess the |
@Kodiologist with #2077 |
This. I thought the magic had pretty dubious value to begin with (and especially now that we have It seems like maybe Hy is moving in the direction of hugging Python closely, and moving conveniences elsewhere. This seems like probably the right approach to me, since it will become easier to make Hy itself stable that way. In that case, the |
That's surprising to me. I guess |
This kind of automagic makes me uncomfortable. It's also complicating the compiler and seems to be the cause of #1533.
Python has a very dynamic runtime. You can rewrite existing classes on the fly because they're objects too. You can introducing new methods and delete old ones. Metaclasses may need to do this kind of rewriting internally to function.
But the
__init__
automagic to returnNone
only works in a defclass form. Not if__init__
was declared as a function outside and the attached later.Worse, if some metaclass other than
type
requires an__init__
method to actually return something, it may be impossible to use in Hy.I'm also worried this may cause issues with decorators. Does the automagic even work if
__init__
is decorated? And even worse, what if does work, but the decorator requires a return value in the decorated method? It would be difficult to use that in Hy.I was opposed to this automagic from the start, but we did add this for a reason. Usually automatic returns are more convenient in Hy. But that's an error in
__init__
so it's a pain to remember to always end__init__
methods withNone
in Hy (if the last form happens to returnNone
, as many do, you might not notice the problem right away).Explicit is better than implicit. Let's get rid of the automagic and just use a simple core
definit
macro instead, like this,Which you'd use like
It returns
None
as required by the standardtype
metaclass, anddefinit
is easier to type, thandef __init__
so it'll get used, instead of forgotten.The text was updated successfully, but these errors were encountered: