var ? as alias for doc #1536
-
hi, totally new user. just curious: or other name tested, but failed. though have success with other self function
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 3 replies
-
How does this seem?
IIUC, the reason some of the other attempts didn't work as expected is because Here is an example of a similar use of Also, this page has a section on
On a side note, one can also express as ...and if you aren't familiar with using the key sequence Ctrl-G at janet's repl, please give it a try :)
So for example in this state:
(so the cursor is after
|
Beta Was this translation helpful? Give feedback.
-
it works. thank you very much. sounds like a hidden feature, not any example in doc. maybe it is not recommended to do so. macro, and quote unquote quasiquote... powerful interesting new concepts, to be learned. but, still a bit confused. why type of both is why they
|
Beta Was this translation helpful? Give feedback.
-
thanks for detailed answers. I would like to make such change: or maybe use new name, such as deeptype(), equaldeep(), or === , or setalias() after long time looking at doc: if I can do this, then I woud have deeper understanding of macro and quoting.
|
Beta Was this translation helpful? Give feedback.
-
at first I thought everything is a function, even operator is function. function is easy to extend, once your have a function f(x), then it is easy to get repl:268:> (defmacro deeptype [x] ~(if (get (dyn ',x) :macro) :macro (type ,x)) )
<function deeptype>
repl:269:> (deeptype doc)
:macro # this works
repl:270:> (defmacro deeptype2 [x] ~(deeptype ',x) )
<function deeptype2>
repl:271:> (deeptype2 doc)
:symbol # this does not work
repl:296:> (defmacro == [x y] (
repl:297:((> if (= x y)
repl:298:((> (= (deeptype ',x) (deeptype ',y) )
repl:299:((> false))
<function ==>
repl:300:> (defn fa [] )
<function fa>
repl:301:> (def fb fa)
<function fa>
repl:302:> (= fa fb)
true
repl:303:> (== fa fb)
false # this does not work , should be true
repl:304:> (deeptype fa)
:function
repl:305:> (deeptype fb)
:function
|
Beta Was this translation helpful? Give feedback.
-
Some intuition that might help: Values in Janet can't be macros -- as you can see, the "value" of a macro is just a function. "Macro-ness" is a property of a binding, not an intrinsic property of the function itself. If you think about macro expansion, this kinda makes sense -- the Janet compiler doesn't know the "value" of things deep inside a function at compile time, because the function isn't being called. It only acts on symbols, and there's basically a separate namespace of macros that it knows about. The standard way to alias a binding is: (setdyn 'doc2 (dyn 'doc)) If you don't want to write the quotes, you can use a macro like this: (defmacro alias [dest src]
~(setdyn ',dest (dyn ',src))) Notice that the quotes are in exactly the same place as they are in the un-macrofied version, we just replaced Here's a simpler version: (defmacro alias [dest src]
(setdyn dest (dyn src))
nil) Instead of returning an AST to mutate the current environment, you can mutate the current environment at macro expansion time and then return (defmacro deeptype [x] ~(if (get (dyn ',x) :macro) :macro (type ,x)) ) You call this macro like this: (deeptype doc) That is, you don't quote (defmacro deeptype2 [x] ~(deeptype ',x) ) You added a quote that you don't need. When you call this macro, it's like you're saying: (deeptype 'doc) Which gives you
The correct way to write that is: (defmacro deeptype2 [x] ~(deeptype ,x) ) i.e., it's exactly the same as the un-abstracted call, except with the parameter replaced. |
Beta Was this translation helpful? Give feedback.
-
if I understand correctly, macro is like "statement", macro "do something", and function "return something". but how can I get the result of a macro in a function? can this following function be possible : (defmacro deeptype [x] ~(if (get (dyn ',x) :macro) :macro (type ,x)) )
(defn == [x y] (
if (= x y)
(= (deeptype x)
(deeptype y))
false))
repl:377:> (defn == [x y] (
repl:378:((> if (= x y)
repl:379:((> (= (deeptype x)
repl:380:(((> (deeptype y))
repl:381:((> false))
<function ==>
repl:382:>
repl:383:> (== doc doc2)
true # need to be false
repl:384:> x
repl:384:1: compile error: unknown symbol x
repl:385:> (defn == [x y] (
repl:386:((> if (= x y)
repl:387:((> (deeptype x)
repl:388:((> false))
<function ==>
repl:389:> (== doc doc)
:function
because I think (= doc doc2) is somehow anti-intuition , they should not be equal and return false it is a surprise. I try to see if I can check this surprise using a == function. |
Beta Was this translation helpful? Give feedback.
-
That's not really right -- a macro is a way to run code at compile time that returns new code that can run at runtime. Macros can be statement-y or expression-y, or they can just perform compile-time side effects and not return any interesting code to run at runtime. The thing that you're looking for doesn't really make sense, because
Notice that it has a docstring, a sourcemap, and a flag saying that it's a macro binding. But the When you alias
With a different sourcemap, no docstring, and no macro flag. But the same value! I think what you want to do is, basically, compare environment entries. Is only the "macro" metadata significant for your comparison? Or are two environment entries with different docstrings different as well? What about other metadata? You can create an environment entry with any metadata you want:
But all of these environment entries have the same value -- the only thing that Janet's
You can define an |
Beta Was this translation helpful? Give feedback.
-
same value, different binding... totally new concept, I need some time to get used to this. thank you very much. |
Beta Was this translation helpful? Give feedback.
That's not really right -- a macro is a way to run code at compile time that returns new code that can run at runtime. Macros can be statement-y or expression-y, or they can just perform compile-time side effects and not return any interesting code to run at runtime.
The thing that you're looking for doesn't really make sense, because
doc
anddoc2
are the same value. They're just different bindings. You can look at the binding of a symbol like this: