Skip to content
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

Perplexing keyword behavior in splice #1352

Closed
gilch opened this issue Jul 31, 2017 · 1 comment
Closed

Perplexing keyword behavior in splice #1352

gilch opened this issue Jul 31, 2017 · 1 comment

Comments

@gilch
Copy link
Member

gilch commented Jul 31, 2017

(defn print-args [&rest args &kwargs kwargs]
  (print args kwargs))
=> (print-args :key 42)  ; normal keyword magic
print_args(key=42)
() {'key': 42}
=> (print-args ':key 42)  ; quote to pass keyword as data
from hy import HyKeyword
print_args(HyKeyword('\ufdd0:key'), 42)
('\ufdd0:key', 42) {}

That was strange for a Lisp, but easy enough to understand.

=> (eval `(print-args ~@[:key 42]))  ; raw keyword
from hy.core.language import eval
from hy import HyExpression, HySymbol
eval(HyExpression((([] + [HySymbol('print_args')]) + list(['\ufdd0:key', 42]))))
('\ufdd0:key', 42) {}
=> (eval `(print-args ~@[':key 42]))  ; quoted keyword
from hy.core.language import eval
from hy import HyExpression, HyKeyword, HySymbol
eval(HyExpression((([] + [HySymbol('print_args')]) + list([HyKeyword('\ufdd0:key'), 42]))))
() {'key': 42}

Somehow it became the opposite? Is this how it's supposed to work?

@Kodiologist
Copy link
Member

Kodiologist commented Jul 31, 2017

I think so. The trick is that in Hy, unlike most Lisps, keywords don't evaluate to themselves. They evaluate to strings, not HyKeywords. So the expression `(print-args ~@[:key 42]) evaluates to (print-args "\\ufdd0:key" 42); if you evaluate this, the compiler will see the string "\\ufdd0:key" instead of a literal keyword. By contrast, `(print-args ~@[':key 42]) evaluates to (print-args :key 42). If you want to pass a real HyKeyword to print-args here, you need two single quotes (i.e., `(print-args ~@['':key 42])) because you're evaluating :key twice, once with ~@ and once with eval.

vodik added a commit to vodik/hy that referenced this issue Mar 25, 2018
HyKeywords are no longer an instances of string with a particular
prefix, but a completely separate object.

This means keywords no longer trip isinstance str checks, adding a
little bit of type safety to the compiler.

It also means that HyKeywords evaluate to themselves.

Closes hylang#1352
vodik added a commit to vodik/hy that referenced this issue Mar 25, 2018
HyKeywords are no longer an instances of string with a particular
prefix, but a completely separate object.

This means keywords no longer trip isinstance str checks, adding a
little bit of type safety to the compiler.

It also means that HyKeywords evaluate to themselves.

Closes hylang#1352
vodik added a commit to vodik/hy that referenced this issue Mar 31, 2018
HyKeywords are no longer an instances of string with a particular
prefix, but a completely separate object.

This means keywords no longer trip isinstance str checks, adding a
little bit of type safety to the compiler.

It also means that HyKeywords evaluate to themselves.

Closes hylang#1352
vodik added a commit to vodik/hy that referenced this issue Apr 4, 2018
HyKeywords are no longer an instances of string with a particular
prefix, but a completely separate object.

This means keywords no longer trip isinstance str checks, adding a
little bit of type safety to the compiler.

It also means that HyKeywords evaluate to themselves.

Closes hylang#1352
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants