Arc Forumnew | comments | leaders | submitlogin
Path for .arc modules
2 points by hasenj 5150 days ago | 3 comments
This is a follow up on my earlier question: http://arclanguage.org/item?id=12744

1. Is it possible to get the path of the current file auto-magically? Something like __FILE__ from C/C++

2. Is it possible to get the path where arc is installed?

If I want to load something from "lib/", how do I do that when my current .arc file is in a completely different directory?



1 point by rocketnia 5148 days ago | link

I could be wrong, but I think there's no widely adopted way to do those things, even in Anarki. I think most people just use a rather shallow directory structure and spell out (load "lib/something.arc") every time. Note that (load "...") resolves the path relative to Racket's (current-directory), which is usually the working directory Racket was started under. So typically people just start Racket from their Arc directory and don't bother messing with environment variables.

That doesn't mean it's a good situation here. :-p In particular, if someone ever wants to use multiple versions of the same library with the same Arc installation (loading them conditionally, maybe), I think the library'll need to be internally location-agnostic; you can't just put both versions in the same directory, after all. ^_^

So in Lathe I go to some trouble to make an alternative 'load function, 'loadrel, just so I can keep track of the current directory. It uses a global variable, 'load-dir* , to keep track of the current file's directory, and that variable is maintained by the functions 'loadrel and 'loadfromwd ("load from working directory," merely a 'load-dir* aware version of Arc's regular 'load).

If you want to try out my code, the necessary definitions are 'loadval from arc/modules/modulemisc.arc and 'split-at-dir, 'normalize-path, 'load-dir* , 'loadfromwd, and 'loadrel from arc/modules/path.arc. It's probably easier just to load the whole Lathe module system, which 'loadrel is a part of:

  (= lathe-dir* "lib/lathe/")
  (load:+ lathe-dir* "loadfirst.arc")
However, I'm noticing that the code's a bit more buggy than I'd like. XD In particular, I've got to fix this right away:

  arc> (normalize-path "a//c/../b.arc")  ; correct
  a/b.arc
  arc> (normalize-path "a.arc")  ; incorrect
  a.arc/
If you're going to implement something like this yourself, I recommend dropping to Racket and using Racket paths to avoid the trouble of things like my 'normalize-path. For a more comprehensive treatment, maybe someone could patch 'load to interact with Racket's 'current-load-relative-directory? Would that even make sense?

-----

1 point by hasenj 5150 days ago | link

I think I found a reasonable hack for #2:

  (def import (path) (load ($.build-path ($.getenv "arc_dir") path)))
Given that you add 'export arc_dir' to arc.sh

-----

1 point by hasenj 5147 days ago | link

Maybe instead of arc_dir, we could use ARC_PATH (similar to PYTHONPATH) which would hold a list of paths separated by colons, the first thing being the directory where arc is installed.

-----