(To expand macros until there are no macros You can expand a macro once in Janet using We want the maximum to be 14, but this will actually evaluate to 12! This can be Now we have no double evaluation problem! But we now have an even more subtle ( defmacro max2 "Get the max of two values." For example, (max1 (do (print 1)ġ) (do (print 2) 2)) will print both 1 and 2 twice, which would be surprising This isīecause both show up in the macro twice. This almost works, but will evaluate both x and y twice. ( defmacro max1 "Get the max of two values." Ignoring that this could be written as a function, consider the following macro: Sometimes when we write macros, we must generate symbols for local bindings. # -> (def myfunction (fn myfunction (x y z) (print x) (print y) (print z))) Accidental Binding Capture ~( def, name ( fn, name, args, body)) When combined with the unquote special, we get the desired output: Special form for this purpose, and a shorthand for it, the character. Want to put the body inside the form (fn args. There is an extra set of parentheses around the body of our function! We don't # -> (def myfunction (fn myfunction (x y z) ((print x) (print y) (print z)))) See what happens if we use a normal unquote for Similar to name, we must also unquote body. Returned tuple, and every function we defined would be called name! Without the unquote, the symbol name would be put in the Name and args is an unquote, which allows us to put a value in the X) except we can unquote expressions inside it. ![]() Shorthand for the (quasiquote x) special form, which is like (quote In such a way that the macro output is more clear. This is functionally identical to our previous version defn2, but written ~( def, name ( fn, name, args, body))) ( defmacro defn3 "Defines a new function." If someone is using the function later, they can use (doc defn3) We can still improve this macro even more though. Great! Now we can define functions with multiple elements in the body. ( tuple ' def name ( apply tuple ' fn name args body))) We can make the macro variadic, just like aįunction. The first issue is that our defn1 macro can't define functions with ![]() There are a couple of issues with this macro, but it will work for simple ( tuple ' def name ( tuple ' fn name args body))) A simple version of the defn macro can be thought We say the macro has been expanded after theĬompiler evaluates it. When the compiler sees a macro, it evaluates the macro and Macros let you extend the syntax of the language itself. Rather than data, so it is more flexible in what it can do than a function. ![]() A macro is like a function, but transforms the code itself For example: /// Some documentation commentĪnd it is possible to capture attributes in macros.Janet supports macros: routines that take code as input and return transformedĬode as output. It is not widely-known, but Rust documentation is actually represented as a special kind of attribute on an item. It is possible to capture doc comments in macro invocations. What are your recommendations for handling this? The best solution for me would be to be able to write specific documentation for each macro invocation, but if that's not possible I would be grateful for hints on how to expand tokens in documentation comments. The only alternative left is to write very generic documentation in the macro, but that would lead to my library being a lot worse documented than it could be. However, when doing that the Rust macro system doesn't expand the token $name in the documentation comment. / Some more generic documentation for $name However, Rust won't generate documentation for CertainType when this happens.Īnother (not as flexible) alternative would be to do something like: macro_rules! new_type (($name:ident, $bytes:expr) => ( The best thing would be if I could write my documentation right before my macro invocation. However, I would also like to document these new structs. I have a couple of macros to reduce boilerplate when defining certain tuple-structs of the form: macro_rules! new_type (($name:ident, $bytes:expr) => (
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |