This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Shell-style programming in Kawa


This message is about setting/getting the current path.

On 01/27/2014 05:06 PM, Jamison Hope wrote:

Similarly, a Scheme interface to the Path.currentPath stuff, like

(chdir "..") => Path.setCurrentPath(Path.currentPath().resolve("..").getCanonical());
(cwd) => Path.currentPath();

could be nice.  Scsh has them, along with a function version of with-cwd that
takes a thunk as its second argument.

I implemented (current-path) asa parameter.

Thus instead of cwd you have currentpath; instead of (chdir "..")
you can use a setter: (set! (current-path) "..") or
(current-path "..").

(define-syntax with-cwd
   (syntax-rules ()
     ((with-cwd wd e ...)
      (let ((old-path (cwd)))
        (try-finally
         (begin (chdir wd) e ...)
         (chdir old-path))))))

(define (with-cwd* wd thunk)
   (let ((old-path (cwd)))
     (try-finally
      (begin (chdir wd) (thunk))
      (chdir old-path))))

We could add these as syntactic sugar, but since
parameterize now works I think that is good enough:

(parameterize ((current-path (path "..")))
   &sh{echo pwd is `pwd`})

I'm still pondering whether the initial (current-path) - i.e. the
value of Path.useDirPath should be "." or the expanded user.dir.
Certainly it seems weird and uninformative for (current-path) to
just print ".".  But perhaps that would be ok if we added a
(path-absolute PATH) function.


The resolve-uri call in chdir depends upon the patch to Path.java I just sent.
Without that, this works instead: (resolve-uri (path (->String dir)) (cwd)).
I tried just capitalizing the s in string, not wrapping it in (path ...),
but that gives me a spurious warning:

warning - no known slot 'getCanonical' in java.lang.Object


It seems like the ->string version ends up calling Path#resolve(Path),
while the ->String version (without the wrap) calls Path#resolve(String),
which is abstract.  Does the compiler ignore the declared return type
of an abstract method when doing its type inferencing?

I believe so. Perhaps we need to do more "target typing" in the Java 8 world.


[Would the compiler be able to inline away the lambda if with-cwd were defined
as

(define-syntax with-cwd
   (syntax-rules ()
     ((with-cwd wd e ...) (with-cwd* wd (lambda () e ...))))))

or does that require a corresponding Procedure.validateApplyKey property on
with-cwd*?

The latter.  There is currently no mechanism to automatically inline
previously-compiled functions.  It would certainly be handy.

Can those be implemented in/for functions written in Scheme?]

Yes.  See with-exception-handler for an example where the hook is
done in Scheme. Though the actual validateApplyWithExceptionHandler
method is written in Java, I can't think of a reason it couldn't
be written in Scheme.  It would help to have a small library of
helper functions - and macros - for munging Expression trees.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]