-
Notifications
You must be signed in to change notification settings - Fork 11
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
assoc for attrs? #35
Comments
Setting attributes of object from parameters in
Arguably it could be called to In any case, I would keep |
In Rogue TV, I wrote a macro hylang/hy#1119 is also related. |
Also thinking aloud, Maybe we could also look at borrowing something from Considering its partially made it into Python proper with PEP 557, might not be a bad way to do stuff. |
Here's an improved (attach self x y :my_foo a_foo)
;; same as (setv self.x x self.y y self.my_foo a_foo) The first argument gets the attachments. The positional arguments come from locals of the same name, and the kwargs let you change the name. |
Here's an implementation.
I feel like it shouldn't be this hard. I feel like the The models should probably be called things like
Example usage:
The gensyms aren't pretty hylang/hy#1531. We're also getting extra I feel like I'm re-implementing |
Model patterns (#1593) would help. ;)
It's better to use |
Forgot about that one. Revision.
Still works.
That does look promising. I've been looking through the funcparserlib docs. I will try to make time to review hylang/hy#1593, but have been busy with work lately. The associated small PRs may have to come first. |
These days you can use (require hyrule [meth])
(defclass Point3d []
(meth __init__ [x y z]
(setv
@x x
@y y
@z z))) or, more concisely, (require hyrule [meth])
(defclass Point3d []
(meth __init__ [@x @y @z])) |
I'm thinking aloud here and looking for feedback.
This is a gap in Hy that's bothered me for a while, since I saw https://stackoverflow.com/questions/28949486/is-there-a-way-in-the-hy-language-to-use-doto-on-self
The advantage
assoc
has oversetv
is that it avoids repeating the location reference when setting multiple pairs e.g.vs
It's much better than
setv/get
, though this is only slightly better thanit doesn't rely on the
.update
method being present and saves some brackets. Nice, but not essential?But a much more common case in Python, especially when using classes, is multiple assignments to different attrs of the same object.
It's the same kind of repetition. The
self.x
sugar helps a little, but not when the object reference is itself an expression.But we don't have a convenient
.update
method, so the use case is even stronger. You could access the backing dict and do something like(.update (vars self) (vars))
, but that feels a little dirty. It gives you aself.self
for one thing (and any other local you've set, including hy_anon stuff and gensysms). And you can't specify different names for the attr than the parameter. We can do better:And even better with
assoc
But we still have to call
vars
and we still have to quote the attr names. This is probably the best we can do without introducing a new macro.You might think you could use
doto
for updating an object, but it's not that easy:doto
really only works for method calls. This really isn't any easier to type. Do we want to force people to write accessors so they can usedoto
? (Or provide a macro to write them?) I think not, for better interop with existing Python modules. Python is not Java.doto
was good enough for Clojure, when you always expect accessors. But you just can't expect that in Python.But if we had some kind of an assoc for attrs, it would just be--
Easy. We'd want a shorter name than
assoc-for-attrs
though.Is there some easier way to do this I'm not seeing? Is
assoc
worth keeping in the first place over.update
? Just to avoid some quotes and brackets? And if so, why don't we have the arguably more important assoc-for-attrs?But maybe we could do even better than that. Parameter names often are the same as the attr names. You shouldn't have to say it twice if the names are the same. We could have a
local-to-attr
form,But again, maybe with a shorter name. We could even combine the two somehow, but it would complicate the macro. Here's one possibility
Better ideas?
The text was updated successfully, but these errors were encountered: