~technomancy/fennel#103: 
Improve relative require

For Fennel 1.0 we made sure that it was at least possible to use relative requires, but the method for doing it is awkward and difficult to remember.

Would it be possible to make it as simple as this?

(local access (require (relatively :access))) ; becomes "current-module.access"
(import-macros {: reinit} (relatively :access.macros))

I guess the main thing that's missing here is how do you relatively require something that is in the same directory as the current module. Perhaps a second argument to relatively could indicate that.

Status
REPORTED
Submitter
~technomancy
Assigned to
No-one
Submitted
1 year, 8 days ago
Updated
3 months ago
Labels
enhancement

~andreyorst 7 months ago

From https://github.com/bakpakin/Fennel/pull/423:

(macro rel-path [module from-macro?]
  `(.. (or (: (or ... "") :match "(.+%.)[^.]+")
           (if (= ... ,(if from-macro? "init-macros" "init"))
               ""
               (.. ... ".")))
       ,module))

This macro should cover most common cases for relative require from regular modules and macro modules.

~technomancy 7 months ago

This looks good, but it seems to assume that macros always use init-macros.fnl which isn't really accurate; the default macro path allows for init-macros.fnl or init.fnl; the former is only needed when the latter is already being used by a runtime module.

~andreyorst 3 months ago

Had to update the macro above because it stopped working:

(macro rel-require [module macro?]
  `(if (: (or ... "") :match "(.+%.)[^.]+")
       (require (.. (: (or ... "") :match "(.+%.)[^.]+") ,module))
       (= ... ,(if macro? "init-macros" "init"))
       (require ,module)
       (require (.. ... "." ,module))))

Without this change it is compiled to IIFE, and not static.

~andreyorst 3 months ago

The technique from the above comment is now part of cljlib. I will monitor if it breaks.

Register here or Log in to comment, or comment via email.