Giter Site home page Giter Site logo

Comments (8)

technomancy avatar technomancy commented on September 15, 2024 2

We can reopen this if there are still questions or if there's something actionable but I think this is answered?

from fennel.

technomancy avatar technomancy commented on September 15, 2024 1

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.

technomancy avatar technomancy commented on September 15, 2024 1

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.

alexaandru avatar alexaandru commented on September 15, 2024 1

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.

andreyorst avatar andreyorst commented on September 15, 2024

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.

andreyorst avatar andreyorst commented on September 15, 2024

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.

andreyorst avatar andreyorst commented on September 15, 2024

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

mitchellwrosen avatar mitchellwrosen commented on September 15, 2024

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)

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.