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
Docs not fully updated for Keyword.__call__
#2157
Comments
Keyword.__call__
To be clear: in recent versions of Hy, you rarely want to use actual keyword objects as the keys of a dictionary. Use strings instead. Note that constructs like |
Thanks, that makes sense. The documentation wasn't very clear on the idiomatic notation for dicts, or at least I missed that part. I'm used to clojure, so I might be biased here :) - I assume any examples in the documentation should be rewritten to use string keys or the
However, doesn't Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>>
>>>
>>> def f(): return {"x": 1}
...
>>> def g(): return dict(x=1)
...
>>> dis.dis(f)
1 0 LOAD_CONST 1 ('x')
2 LOAD_CONST 2 (1)
4 BUILD_MAP 1
6 RETURN_VALUE
>>> dis.dis(g)
1 0 LOAD_GLOBAL 0 (dict)
2 LOAD_CONST 1 (1)
4 LOAD_CONST 2 (('x',))
6 CALL_FUNCTION_KW 1
8 RETURN_VALUE
>>> Unfortunately, there is a significant performance difference between the two - or at least that is still the case on python 3.8
I thought this is was worth mentioning (but in general I don't this is much of a big problem) -- I agree that the real problem seems to be that the documentation is not clear enough on the fact that keywords are not meant to be used as general purpose objects, but rather exist only to facilitate keyword arguments. |
Right.
Right, the Hy equivalent of
That's a fair point. Although you can use Hy keywords much as you can keywords in other Lisps, it requires a little care because e.g. |
Trying to access a dict keyed by a keyword (as presented in some more complex examples from the documentation e.g. https://docs.hylang.org/en/alpha/api.html?highlight=person#fn) does not seem to work.
Using the
(get obj key)
form seems to work howeverThis seems to be a consequence of
hy/hy/models.py
Line 254 in 949e07b
(:kw d)
is effectivelyd["kw"]
after mangling+stringification.Perhaps it would be reasonable to introduce a two-step lookup for keywords i.e. where keyword lookup is attempted first, and "mangled string lookup" is attempted if that does not work? Perhaps something like this would be a reasonable compromise:
{:x 1 "x" 2}
would then become ambiguous but in this case the user can utilize(get {:x 1 "x" 2} :x)
or(get {:x 1 "x" 2} "x")
to disambiguate.The text was updated successfully, but these errors were encountered: