REBOL 3.0

Comments on: Defining DIR?

Carl Sassenrath, CTO
REBOL Technologies
6-May-2010 14:17 GMT

Article #0319
Main page || Index || Prior Article [0318] || Next Article [0320] || 10 Comments || Send feedback

The dir? function is intended to be an easy-to-use way to check if a file or file path is a directory.

For example, when processing a list of files we often write:

foreach file file-list [
    unless dir? file [
        process-file file
    ]
]

The problem is that dir? does two things:

  1. It checks if the filename ends with a slash (/).
  2. It checks if the file is a directory on local storage.

This behavior is problematic because the filenames being checked may have nothing to do with local storage. In the example above, the list of files may have been fetched from a server or a database, and they're not related to what's on disk.

So, we need to define dir? a little better. It should do one of the above operations, but not both.

Personally, I'd prefer the first behavior, because that's what I use the most. I never do the second, because I'm using filenames that were obtained from a directory, so they contain the slash in their names. However, that's not compatible with R2.

The second behavior could be supported with a refinement, like dir?/query which would perform the actual disk query.

10 Comments

Comments:

Maxim Olivier-Adlhoch
6-May-2010 11:31:42
isn't DIR? already just checking the path?

I thought we had resolved that one a few years ago...

in A96:

>> dir? %/path/does/not/exist/
== true

PLEASE do not re-introduce the old R2 behaviour its useless.

it got replaced by exists? returning 'dir 'file or none

>> exists? %/path/does/not/exist/
== none
Carl Sassenrath
6-May-2010 11:38:41
Hi Maxim, no it does both... but you may not have noticed because it did the path string check first.

However, bug #602 shows the problem. A file %foo could be a directory on disk, in which case DIR? returns true.

My proposal is to simplify, and make it just check the path string and not go to disk.

Maxim Olivier-Adlhoch
6-May-2010 13:46:33
oh... well, the current behavior is even less meaningull then ;-)

yep, slash the file access, and I don't see any need to add the /QUERY refinement.

we've got EXISTS? for that, and its a sexy function which you can even use directly with SWITCH or CASE.

Carl Sassenrath
6-May-2010 13:54:49
Sounds good. A99 will change it.

If anyone has opposing opinions, post them really soon.

Maarten
6-May-2010 16:57:55
Add a refinement for a comparator if needed (or use generator for dir? closures) Comparator as in a sort function.
Graham
7-May-2010 1:35:02
If the purpose of dir? is to check for a trailing /, then it's misnamed. Perhaps rename it to tail/? or something.
Brian Hawley
7-May-2010 4:42:42
Adding a refinement for a comparator is a little heavy for what would be a one-line mezzanine - what do you mean Maarten, in REBOL code? And tail/? isn't a valid word Graham, it's a path, so "or something" would be better.

Having dir? just work on the filename, and exists? check the file works for me. We need both operations on different occasions, whatever we end up calling dir?.

Kaj
7-May-2010 9:16:42
Just to say I'm relieved at this decision.

I would also like to mention that in the beginning, I was confused by file? being a type check and dir? checking storage. Having dir? act as a subtype check is much more elegant.

Graham
7-May-2010 19:53:05
Having dir? check for a trailing / means it is true for a string such as "sfsf/" but false for "c:\windows\" ... so I think a better name is needed.
Brian Hawley
8-May-2010 5:52:55
The dir? function checks for both / and \, so your concern is handled, Graham.

Post a Comment:

You can post a comment here. Keep it on-topic.

Name:

Blog id:

R3-0319


Comment:


 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote
 
 

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

REBOL 3.0
Updated 24-Mar-2017 - Edit - Copyright REBOL Technologies - REBOL.net