chris-martin / bricks

Bricks is a lazy functional language based on Nix.
https://hackage.haskell.org/package/bricks
6 stars 1 forks source link

A way to obviate path literals #10

Open chris-martin opened 6 years ago

chris-martin commented 6 years ago

In Nix, if an unquoted string contains a slash but does not start with a slash, then it is interpreted as a relative path with respect to the directory in which the source file resides, and the parser converts it to an absolute path.

I love being able to easily do relative imports, but I don't find this approach particularly elegant or intuitive, and I don't like how it complicates the syntax.

But, I just realized, we can get relative imports easily without a language feature.

The import function in the "standard library" dict that we pass to a file's top-level function just needs to include the path of the file (and probably some additional convenient path-manipulating functions). In doing this, we buy back some purity. By passing the path argument through a function parameter, we eliminate a case where an expression's semantics depend on something other than formal parameters.

The other cool thing about this: While Nix import syntax is restricted to static paths only,

A Nix expression loaded by import must not contain any free variables (identifiers that are not defined in the Nix expression itself and are not built-in). Therefore, it cannot refer to variables that are in scope at the call site.

Bricks won't have that limitation. import will, syntactically, be an ordinary function.