Comments (8)
We can reopen this if there are still questions or if there's something actionable but I think this is answered?
from fennel.
Right now it is an unknown identifier error, but it looks like it should be a syntax error instead.
I thought we already did treat this as a parse error; apparently not. I agree it should be one, but I'm concerned about the backwards-compatibility implications of making it invalid now.
Probably my preferred way would be this:
(let [{: setup} (require :foo)] (setup))
Admittedly slightly more verbose. Frequently in Lua the idiom is to call this as a method instead, which lets you do this:
(doto (require :foo) (: :setup))
which is much nicer. I might be tempted to do this anyway even if the self arg is ignored.
from fennel.
The examples I provided are just running fennel directly
It's mentioned elsewhere but one thing that might not be obvious is that AOT compilation (fennel --compile foo.fnl
) does not perform strict global checking unless you ask for it with --globals
:
~/src/fennel $ ./fennel --compile scratch.fnl
return require("foo")(__fnl_global___2esetup)
~/src/fennel $ ./fennel --globals _G --compile scratch.fnl
scratch.fnl:1:16 Compile error: unknown identifier: .setup
((require "foo").setup)
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G..setup if you really want a global.
* Try moving this code to somewhere that .setup is in scope.
* Try binding .setup as a local in the scope of this code.
This was originally because we assumed you might be compiling under (for instance) PUC Lua code which was meant to run under LuaJIT; nowadays I think it was a mistake, but it's too late to change unfortunately.
from fennel.
Tip: you can use antifennel
to see how a piece of Lua code would look like in Fennel, i.e.:
$ echo 'require("myplugin").setup()'|antifennel -
((. (require :myplugin) :setup))
Hope it helps, cheers!
from fennel.
The third attempt is the correct one if you want to do it in a one-liner, though you can use colon strings:
((. (require :foo) :setup))
the do end
in the beginning is needed because if you had an identifier before the (require("foo")).setup()
it will be interpreted by Lua as a function call. E.g.:
foo
(require("foo")).setup()
Lua sees it as foo(require("foo")).setup()
not as foo; (require("foo")).setup()
. So do end
is like a ;
but more portable between all Lua implementations and is compiled away in bytecode.
from fennel.
The reason the documentation mentions .
only with symbols is that you can always use .field
when you statically know the key, and .
is used when you don't know the key statically or the table is not lexically bound and is produced by a function call.
from fennel.
@technomancy I think the Fennel compiler should report this ((require "foo").setup)
as an error though.
unknown:2:16 Compile error: unknown identifier: .setup
((require "foo").setup)
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G..setup if you really want a global.
* Try moving this code to somewhere that .setup is in scope.
* Try binding .setup as a local in the scope of this code.
Right now it is an unknown identifier error, but it looks like it should be a syntax error instead.
And it's also weird that the ((. (require "foo") setup))
didn't trigger the unknown global error, but this may be Nvim thing, as they can disable strict global checking
from fennel.
Cool, thank you for the info!
One point:
And it's also weird that the ((. (require "foo") setup)) didn't trigger the unknown global error, but this may be Nvim thing, as they can disable strict global checking
There's no nvim in-between here. The examples I provided are just running fennel
directly on Lua snippets.
from fennel.
Related Issues (20)
- `comment` macro can leak some code HOT 2
- Incomplete `for` binding table results in bad code generated HOT 1
- Macro treat vararg as first parameter... Unless quoted and unqoted. HOT 5
- Compatibility with Pluto HOT 6
- Cannot use &as with a rest parameter. HOT 2
- Expose the 'varg' function to the compiler environment HOT 4
- `,apropos-doc` should search repl locals too HOT 1
- `,apropos-show-docs` not working HOT 1
- Bad code generated in the REPL if the name of a local contains a backslash
- Luau syntax support HOT 4
- [Enhancement] Allow passing special forms as functions in macro context HOT 3
- [Feature Request] Global macro definitions HOT 1
- [Stdlib] Add type check macros HOT 1
- readline support HOT 10
- macro question: access a function's form inside a macro HOT 1
- Creating macro which unpacks arbitrary number of arguments yields incorrect result HOT 2
- Cannot rename argument when using docstring and metadata at the same time in lambda HOT 1
- Line numbers in errors and stack incorrect in `dofile` HOT 2
- Hello! Is about the style.md file on the main repository HOT 4
- REPL doesn't store `gensym`'ed variables? HOT 3
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.