Comments (5)
This is one of the most confusing things about Fennel; unfortunately we can't do much about it since we're stuck with this from Lua.
However, there's really no reason to ever use ...
in macros. You seem to be assuming it works like a table, but if what you want is a table, you can get a table using &
:
(macro vararg-macro [& args]
(case args
[a b] b
x x))
Perhaps we should update our documentation to prefer this style since it's so much less confusing.
from fennel.
I've updated the docs to use the nicer style for rest args: https://git.sr.ht/~technomancy/fennel/commit/7d5d8fa575ca087ebfd16b2d19291df2feb811aa
I think we can close this out but feel free to ask if anything else isn't clear.
from fennel.
from fennel.
@andreyorst also why ...
is interpreted as 1
instead of {1 2}
and (type ...)
as "number"
instead of "table"
?
It seems like a bug that we can pattern-match on number and successfully match table.
from fennel.
I think you are misunderstanding how ...
works. ...
evaluates as (values 1 2)
, not 1
nor [1 2]
. (type ...)
expands to (type 1 2)
, and since type only reads its first argument, it evaluates to number
.
When you quote and unquote it, you are also wrapping it in a list, which is why you see the change in your code's behavior.
`(,...)
;; is the same as
(list ...)
You can use []
patterns to destructure lists and tables, but you need to use ()
patterns to match multiple values.
...
works the same way outside of macros, and inherits its behavior from lua:
local function example_fn(...)
print(...) -- => 1 2
print(type(...)) -- => number
local x = ...
print(x) -- => 1
local args = {...} -- => {1, 2}
end
example_fn(1, 2)
from fennel.
Related Issues (20)
- Inconsistent handling of commandline arguments HOT 3
- Macro question: Multiple value binding for `local` HOT 5
- Typo in the Wiki page HOT 1
- Official logo/devicon HOT 7
- Strict globals forbid correct code HOT 5
- No --add-package-cpath option HOT 2
- Failing to compile certain Luakit config HOT 5
- Surprising `match` result on Sequential Table HOT 7
- Emacs: reloading `require` lines HOT 9
- Strange compile error occurred when iterating a table generated by macro in another macro HOT 3
- The require("foo").setup() idiom seems hard to translate to fennel HOT 8
- Key/value table order doesn't seem deterministic HOT 3
- Issue when strings containing @ are simplified HOT 3
- Is 'utf8' module required now? HOT 1
- `comment` macro can leak some code HOT 2
- Incomplete `for` binding table results in bad code generated HOT 1
- Compatibility with Pluto HOT 6
- Cannot use &as with a rest parameter. HOT 2
- Expose the 'varg' function to the compiler environment HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fennel.