-
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
:keywords as objects #846
Comments
👍 👍 |
The other alternative is to not have keyword objects. Python doesn’t have keyword objects.
|
That is certainly an option too. But it looks a bit weird to have |
Almost two years in, and having Hy depolyed into production at various places, it turns out that I can live without keywords as objects. As noone else stepped up to implement them either, lets get rid of this issue! |
After #1352, I want to think about this some more. Keywords as (invalid) strings are kind of weird. They're awkward in macros. Leaking
I think Python could have used a good symbol type. But keywords on top of that actually don't seem that useful in Hy. But then how would kwargs work? Clojure does have a separate keyword type, but in other Lisps, keywords are just symbols that start with Unifying keywords and symbols would probably simplify the compiler and macros. We could also make symbols callable, so |
Is there a reason we can't just make it a compile error to use keywords outside of a keyword context or macro? IMO I really can't think of a major use case for them that wouldn't be complicated by their existing usage. |
I don't understand how that could possibly work. Hy can use keywords to name kwargs in function calls, and as control words special forms, like Macros should at least be able to do the same as special forms. Macros must be able to build any We could use a separate |
No need to add a prefix anymore, its a completely distinct type. See hylang#846
No need to add a prefix anymore, its a completely distinct type. See hylang#846
No need to add a prefix anymore, its a completely distinct type. See hylang#846
Now that #1543 is merged, what do we think about making them callable like in Clojure? We can do the same for symbols, but strings and displays would be harder, requiring compile-time hacks that might not even work that consistently #963. An alternative might be a tag macro that makes the basic collections callable. E.g.
Of course, this would make a string work like a list of characters.
Which may or may not be what the user expects. Strings are not callable in Clojure, unlike symbols and keywords. But if keywords become callable, perhaps they could look up the equivalent string instead of itself?
But since a symbol is-a string in Hy, if we make symbols callable, they'll do that naturally. So keywords could be restricted to looking up keywords as in Clojure. Another option is to make HyString callable. This way you can use a string literal as a lookup function if you quote it.
|
Keep in mind that using a callable keyword literal to a higher-order function would require quoting it, since |
Ah, yep. That does make them seem a bit less convenient as functions, since that would be easy to forget. Until you do it a lot. But it would certainly be possible to pass them to higher-order functions. But I think we're already using a compile-time hack to make them seem callable.
I think we could remove that special case from the compiler without losing the functionality if keywords were properly callables. It seems weird that the above works, but not as higher-order functions and not with symbols. I'm not sure how useful keyword-keyed dicts are in Hy anyway. Python nearly always uses strings for this kind of thing. Maybe it's a bit easier to type, but I'm not really seeing an advantage over using symbols, which count as strings anyway. |
Man, I completely forgot that was implemented. I wonder if it's even documented. |
Two questions about keywords not being available in macros. (Seems to be part of this issue here, sorry, if wrong.)
Why is that? Also, the merged PR above does not provide keywords in macros, right? |
But keywords are available in macros. Do you just mean that macros can't have keyword arguments (in the Python sense of the word "keyword")? In that case, yes, #1581 doesn't change that as #1581 currently stands.
This is no longer the case since #1543. What was annoying about it was that it meant keywords didn't evaluate to themselves (as in other Lisps), and so you had to distinguish between HyKeyword objects and the result of evaluating a keyword, which was a string distinguished from ordinary strings by its value rather than its type. |
The main reason keywords are invalid unicode strings is that we didn't want to leak Hy objects into python. Having used Hy in production for a while, and planning to deploy it yet another place, I do not see an issue with leaking Hy objects into python space.
Therefore, I propose we turn keywords into proper objects!
Pros:
(string? :keyword)
would do the right thing.(map :keyword [{:keyword "found me!" :closet :skeleton}])
=>("found me")
Cons:
The text was updated successfully, but these errors were encountered: