[e-lang] Re: Question about creating directory in E

Mark S. Miller markm at cs.jhu.edu
Tue Dec 14 15:55:01 EST 2004


francis tsang wrote:
> I got the following problem in E. I want to create a directory with 
> the following code in E. When I run the code, the current directory 
> is c:/cygwin/home/francis/jhu/thNetCom/project/
> 
> The subdirectory testing does not exist when I run the code.

[Note: I edited the following slightly, as I think you may have made a 
cut-and-paste error. Could you please check? Thanks. --MarkM]

> $ rune
> ? def dir := <file: "testing">
> # value: <file:c:/cygwin/home/francis/jhu/thNetCom/project/testing>
> 
> ? dir.mkdirs()
 > # problem: <NoSuchMethodException: <a File>.mkdirs/0>
> 
> Do you know what is going on? Thanks a lot.

Yes. And my apologies for the fact that the annotated javadoc at erights.org 
is slightly out of date.

For your example, it will work to do

     dir.mkdirs(null)

This extra (typically 'null') argument must be used for any of the following 
methods on File objects:

     createNewFile/0, delete/0, mkdir/0, mkdirs/0, renameTo/1,
     setLastModified/1, and setReadOnly/0

where "foo/n" is our notation for "the message name 'foo' with n arguments".

The problem with all these, as defined by Java, is that they often indicate 
failure by returning false, rather than the normal Java style of throwing 
exceptions. Since most existing APIs indicate failure by throwing exceptions, 
we found that this deviation was causing us to miss failures, as we'd forget 
we'd need to check the return values.

If you provide 'null' as the extra argument, then these calls will throw an 
exception on failure. If you want to simulate the old Java behavior, you can 
do that as follows:

     def result := escape ejector {
         dir.mkdirs(ejector)
     } catch ignore {
         false
     }

The precise definition of these methods can be found in the source file 
FileSugar.java. For example, the precise definition of the new mkdirs is

     /**
      * Like {@link File#mkdirs()}, but, unless self is already a directory,
      * under conditions where that method would return false, this one instead
      * does a {@link Thrower#eject non-local exit} according to optEjector.
      * <p/>
      * If self is already a directory, then this just returns false. If self
      * is not already a directory, and if this succeeds, then it returns true.
      */

For even more on this issue that you probably want to know, see 
<http://www.eros-os.org/pipermail/e-lang/2004-June/009859.html>.

-- 
Text by me above is hereby placed in the public domain

     Cheers,
     --MarkM



More information about the e-lang mailing list