Olical / aniseed

Neovim configuration and plugins in Fennel (Lisp compiled to Lua)
https://discord.gg/wXAMr8F
The Unlicense
606 stars 28 forks source link

Module still needs export table? #88

Closed Grazfather closed 2 years ago

Grazfather commented 2 years ago

With the module macro and using the require or autoload key in the macro, I'd expect to not need to put a table at the end of my module, but if I don't, I get this error:

Error detected while processing /Users/g/.local/share/nvim/site/pack/packer/start/aniseed/pl
ugin/aniseed.vim:
line    2:
E5108: Error executing lua [string "luaeval()"]:1: Vim(echoerr):E15: Invalid expression: .../site/p
ack/packer/start/aniseed/lua/aniseed/autoload.lua:28: attempt to index a function value
stack traceback:
        [C]: in function 'init'
        [string "luaeval()"]:1: in main chunk
Press ENTER or type command to continue

My core/init.fnl has:

(module core.init                                                                                
  {autoload {: packer                                                                            
             utils core.utils}                                                                   
   require-macros [core.macros]})     

(utils.something "hello")                                                           

and my core/utils.fnl has:

(module core.utils)

(fn something
  [a]
   a)

; {: something}

Uncommenting the last line fixes the issue. Is this expected behaviour? The docs suggest that I shouldn't need to do this anymore.

Olical commented 2 years ago

This is because you're using fn over defn I think. The defn macro (alongside the module macro) essentially create and pre-export the final table as the module loads, so you don't need the table at the end. By the time Lua gets to the end of the file it realises a table is already in memory for this module and so will return that to whoever required it.

If you just want to use fn instead of the defn macro you will need to return tables like regular Lua / Fennel.

Grazfather commented 2 years ago

Aha! PEBKAC.

Thanks for the clarification. Switching my utils to use defn obviates the need to add the table at the end.