Giter Site home page Giter Site logo

Comments (5)

technomancy avatar technomancy commented on August 11, 2024 2

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.

technomancy avatar technomancy commented on August 11, 2024 1

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.

andreyorst avatar andreyorst commented on August 11, 2024

from fennel.

Renegatto avatar Renegatto commented on August 11, 2024

@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.

XeroOl avatar XeroOl commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.