If you think that is a reasonable behavior for file-name-directory
then consider filing an enhancement request: M-x report-emacs-bug
.
On the other hand, imagine that most of the cases where file-name-directory
is called, it is passed a value that is beyond the control of the immediate code. That is, file-name-directory
can be called in any context, and it could be supposed that in most contexts an argument value of nil
would indeed represent an error and should be raised as such.
It's always a programmer choice - a judgment of which behavior is most useful. Presumably, the Emacs developers who designed file-name-directory
figured that a non-string argument should in general raise an error.
Remember too that it is trivial for code that calls file-name-directory
to control the behavior, so that it is different in the case of a nil
(or other non-string) argument. It could be as trivial as this:
(defun my-file-name-directory (file)
"`file-name-directory', except return `nil' for `nil' argument."
(and file (file-name-directory file)))
Or it could handle the wrong-type-argument
error anyway you like, including special-casing nil
to just return nil
(as in the above code). To do that, just wrap the call in condition-case
and handle the error that would normally be raised anyway you like. This code does exactly the same as the above code, but you can see that you could handle other wrong types individually, by testing them (numberp
, arrayp
, symbolp
, etc.)
(defun my-file-name-directory (file)
"`file-name-directory', except return `nil' for `nil' argument."
(condition-case err
(file-name-directory file)
(wrong-type-argument (and file (error "%s" (error-message-string err))))))