unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* `file-attribute' accessors
@ 2012-04-09 13:56 Lars Magne Ingebrigtsen
  2012-04-09 14:19 ` Thierry Volpiatto
                   ` (2 more replies)
  0 siblings, 3 replies; 22+ messages in thread
From: Lars Magne Ingebrigtsen @ 2012-04-09 13:56 UTC (permalink / raw)
  To: emacs-devel

I was reading code that said `(nth 7 (file-attributes file-name))', and
I had to look up the doc string of `file-attributes' and find item seven
to understand what the code was trying to do.

Wouldn't it be nice if we had accessor functions for these things
instead of making people write `nth 7'?  If the code said
`(file-attribute-size (file-attributes file-name))', that would have
made things more understandable.  (And probably easier to write, too,
since one wouldn't have to consult the `file-attributes' doc string
every time...)

If this sounds OK, I can add the, erm, 11 trivial accessor functions and
adjust the documentation of `file-attributes' in trunk.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 13:56 `file-attribute' accessors Lars Magne Ingebrigtsen
@ 2012-04-09 14:19 ` Thierry Volpiatto
  2012-04-09 14:40   ` Lars Magne Ingebrigtsen
  2012-04-09 15:32 ` Leo
  2012-04-09 16:33 ` Stefan Monnier
  2 siblings, 1 reply; 22+ messages in thread
From: Thierry Volpiatto @ 2012-04-09 14:19 UTC (permalink / raw)
  To: emacs-devel

Lars Magne Ingebrigtsen <larsi@gnus.org> writes:

> I was reading code that said `(nth 7 (file-attributes file-name))', and
> I had to look up the doc string of `file-attributes' and find item seven
> to understand what the code was trying to do.
>
> Wouldn't it be nice if we had accessor functions for these things
> instead of making people write `nth 7'?  If the code said
> `(file-attribute-size (file-attributes file-name))', that would have
> made things more understandable.  (And probably easier to write, too,
> since one wouldn't have to consult the `file-attributes' doc string
> every time...)
Why not keys instead of many accessor functions?

See `helm-file-attributes' in helm-utils.el at
https://github.com/emacs-helm/helm

(helm-file-attributes "~/.emacs.d/emacs-config-laptop/.emacs.el" :size t)
=>62675
(helm-file-attributes "~/.emacs.d/emacs-config-laptop/.emacs.el" :human-size t)
=>"61.2K"

> If this sounds OK, I can add the, erm, 11 trivial accessor functions and
> adjust the documentation of `file-attributes' in trunk.

-- 
  Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 14:19 ` Thierry Volpiatto
@ 2012-04-09 14:40   ` Lars Magne Ingebrigtsen
  2012-04-09 14:53     ` Thierry Volpiatto
  2012-04-09 15:46     ` Ted Zlatanov
  0 siblings, 2 replies; 22+ messages in thread
From: Lars Magne Ingebrigtsen @ 2012-04-09 14:40 UTC (permalink / raw)
  To: emacs-devel

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> Why not keys instead of many accessor functions?

Because `file-attributes' already has an optional parameter, and one
sometimes wants to have, say, both the uid and the size, and statting
the file twice to get that isn't optimal.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 14:40   ` Lars Magne Ingebrigtsen
@ 2012-04-09 14:53     ` Thierry Volpiatto
  2012-04-09 15:46     ` Ted Zlatanov
  1 sibling, 0 replies; 22+ messages in thread
From: Thierry Volpiatto @ 2012-04-09 14:53 UTC (permalink / raw)
  To: emacs-devel

Lars Magne Ingebrigtsen <larsi@gnus.org> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>> Why not keys instead of many accessor functions?
>
> Because `file-attributes' already has an optional parameter, and one
> sometimes wants to have, say, both the uid and the size, and statting
> the file twice to get that isn't optimal.
Without key argument i.e only filename, you have all in a proplist, so
you do not have to call function many times to have diverses infos.

-- 
  Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 13:56 `file-attribute' accessors Lars Magne Ingebrigtsen
  2012-04-09 14:19 ` Thierry Volpiatto
@ 2012-04-09 15:32 ` Leo
  2012-04-09 16:31   ` Stefan Monnier
  2012-04-09 16:33 ` Stefan Monnier
  2 siblings, 1 reply; 22+ messages in thread
From: Leo @ 2012-04-09 15:32 UTC (permalink / raw)
  To: emacs-devel

On 2012-04-09 21:56 +0800, Lars Magne Ingebrigtsen wrote:
> `(file-attribute-size (file-attributes file-name))', that would have

Please name it file-size and possibly file-mtime, file-ctime and
file-atime, which is what I have been doing in my .emacs.

Leo




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 14:40   ` Lars Magne Ingebrigtsen
  2012-04-09 14:53     ` Thierry Volpiatto
@ 2012-04-09 15:46     ` Ted Zlatanov
  2012-04-09 16:29       ` Lars Magne Ingebrigtsen
  2012-04-09 16:32       ` Stefan Monnier
  1 sibling, 2 replies; 22+ messages in thread
From: Ted Zlatanov @ 2012-04-09 15:46 UTC (permalink / raw)
  To: emacs-devel

On Mon, 09 Apr 2012 16:40:51 +0200 Lars Magne Ingebrigtsen <larsi@gnus.org> wrote: 

LMI> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>> Why not keys instead of many accessor functions?

LMI> Because `file-attributes' already has an optional parameter, and one
LMI> sometimes wants to have, say, both the uid and the size, and statting
LMI> the file twice to get that isn't optimal.

I would extend `file-attributes' to take &rest symbols after the extra
format argument, so you could say

(file-attributes FILE nil 'size 'mtime)

And you'd get back a two-element list.  I think that's the most concise
approach: it doesn't create a bunch of new functions, scales from one
attribute to N or "just give me all", and is backwards compatible.

Ted




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 15:46     ` Ted Zlatanov
@ 2012-04-09 16:29       ` Lars Magne Ingebrigtsen
  2012-04-09 16:32       ` Stefan Monnier
  1 sibling, 0 replies; 22+ messages in thread
From: Lars Magne Ingebrigtsen @ 2012-04-09 16:29 UTC (permalink / raw)
  To: emacs-devel

Ted Zlatanov <tzz@lifelogs.com> writes:

> I would extend `file-attributes' to take &rest symbols after the extra
> format argument, so you could say
>
> (file-attributes FILE nil 'size 'mtime)

Yeah, that makes sense.  I kinda like accessor functions, though, since
they make it natural to say stuff like

(reduce '+ (mapcar 'file-attribute-size (mapcar 'file-attributes (directory-files dir))))

but the &rest makes a lot of sense.

While we're bikeshedding, using a plist (as previously suggested) would
also be nice when we're returning lists.  So

(file-attributes FILE nil 'size 'mtime)

could return `(size 3422 mtime (232 42))', perhaps?  Anyway, I think the
optional ID-FORMAT parameter is awkward, and doesn't really belong in
such a low-level access function, in my opinion.  But getting rid of it
is out of the question, I guess?  Although the doc string says

---
ID-FORMAT specifies the preferred format of attributes uid and gid (see
below) - valid values are 'string and 'integer.  The latter is the
default, but we plan to change that, so you should specify a non-nil value
for ID-FORMAT if you use the returned uid or gid.
---

which is going to break stuff, anyway.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 15:32 ` Leo
@ 2012-04-09 16:31   ` Stefan Monnier
  2012-04-09 17:08     ` Drew Adams
  2012-04-09 17:57     ` Leo
  0 siblings, 2 replies; 22+ messages in thread
From: Stefan Monnier @ 2012-04-09 16:31 UTC (permalink / raw)
  To: Leo; +Cc: emacs-devel

>> `(file-attribute-size (file-attributes file-name))', that would have
> Please name it file-size and possibly file-mtime, file-ctime and
> file-atime, which is what I have been doing in my .emacs.

Such name would imply that the arg is a file(name) rather than a set of
file-attributes.


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 15:46     ` Ted Zlatanov
  2012-04-09 16:29       ` Lars Magne Ingebrigtsen
@ 2012-04-09 16:32       ` Stefan Monnier
  2012-04-09 16:52         ` Ted Zlatanov
  1 sibling, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2012-04-09 16:32 UTC (permalink / raw)
  To: emacs-devel

> (file-attributes FILE nil 'size 'mtime)

Too complex.


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 13:56 `file-attribute' accessors Lars Magne Ingebrigtsen
  2012-04-09 14:19 ` Thierry Volpiatto
  2012-04-09 15:32 ` Leo
@ 2012-04-09 16:33 ` Stefan Monnier
  2012-04-09 17:35   ` Lars Magne Ingebrigtsen
  2 siblings, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2012-04-09 16:33 UTC (permalink / raw)
  To: emacs-devel

> If this sounds OK, I can add the, erm, 11 trivial accessor functions and
> adjust the documentation of `file-attributes' in trunk.

I guess that'd be OK.


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 16:32       ` Stefan Monnier
@ 2012-04-09 16:52         ` Ted Zlatanov
  2012-04-10  1:48           ` Stefan Monnier
  2012-04-11  1:55           ` Miles Bader
  0 siblings, 2 replies; 22+ messages in thread
From: Ted Zlatanov @ 2012-04-09 16:52 UTC (permalink / raw)
  To: emacs-devel

On Mon, 09 Apr 2012 12:32:09 -0400 Stefan Monnier <monnier@iro.umontreal.ca> wrote: 

>> (file-attributes FILE nil 'size 'mtime)
SM> Too complex.

...as opposed to `(nth 7)' or a separate function for each attribute?
Seriously?

Ted




^ permalink raw reply	[flat|nested] 22+ messages in thread

* RE: `file-attribute' accessors
  2012-04-09 16:31   ` Stefan Monnier
@ 2012-04-09 17:08     ` Drew Adams
  2012-04-10  1:47       ` Stefan Monnier
  2012-04-09 17:57     ` Leo
  1 sibling, 1 reply; 22+ messages in thread
From: Drew Adams @ 2012-04-09 17:08 UTC (permalink / raw)
  To: 'Stefan Monnier', 'Leo'; +Cc: emacs-devel

> >> `(file-attribute-size (file-attributes file-name))', that 
> >> would have
> > Please name it file-size and possibly file-mtime, file-ctime and
> > file-atime, which is what I have been doing in my .emacs.
> 
> Such name would imply that the arg is a file(name) rather 
> than a set of file-attributes.

Yes, and `file-attribute-size' gives the impression that it is about the size of
a file attribute.  And so on.

I do not see this requested enhancement (adding 11 attribute accessor functions)
as worth it.  It's misguided - just cruft.

For operations that are particularly useful we could instead add a function that
takes the file name as arg and returns a particular attribute value - e.g.
`file-size', `file-type', `file-uid'.

Other than such cases, YAGNI: If you want only one attribute value there is no
reason to clutter your code with (this-is-the-one (give-me-all-of-them FILE))
instead of just (give-me-the-one FILE).

And we probably do not need such a function for each attribute.  E.g. attribute
#9: "t if file's gid would change if file were deleted and recreated".  YAGNI.

If you prefer, you could append "-attribute" to the name: `file-size-attribute'
etc. instead of `file-size' etc.

But in any case these functions would be applied to a filename, the same way
`file-attributes' is, and not to the result of calling `file-attributes'.
(Accessor functions, indeed - bof.)

> > If this sounds OK, I can add the, erm, 11 trivial accessor 
> > functions and adjust the documentation of `file-attributes'
> > in trunk.
> 
> I guess that'd be OK.

Hasty and bad idea, IMHO.

(If you really want to look for enhancements, check the bug list - there are
lots of enhancement requests there that make more sense than this one.  And
`report-emacs-bug' is the way to submit an enhancement request, in any case.)






^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 16:33 ` Stefan Monnier
@ 2012-04-09 17:35   ` Lars Magne Ingebrigtsen
  2012-04-10  1:50     ` Stefan Monnier
  0 siblings, 1 reply; 22+ messages in thread
From: Lars Magne Ingebrigtsen @ 2012-04-09 17:35 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> If this sounds OK, I can add the, erm, 11 trivial accessor functions and
>> adjust the documentation of `file-attributes' in trunk.
>
> I guess that'd be OK.

While writing the doc string to all the functions, it struck me that it
would be kinda nice if the definition of the function would be something
like

(defun file-attribute-size (elem)
  ...
  (if (stringp file)
      (nth 7 (file-attributes elem))
    (nth 7 elem)))

That is, the functions would accept both a string and a file attribute
list, and do the obvious thing in either case.

What do you think?

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 16:31   ` Stefan Monnier
  2012-04-09 17:08     ` Drew Adams
@ 2012-04-09 17:57     ` Leo
  1 sibling, 0 replies; 22+ messages in thread
From: Leo @ 2012-04-09 17:57 UTC (permalink / raw)
  To: emacs-devel

On 2012-04-10 00:31 +0800, Stefan Monnier wrote:
> Such name would imply that the arg is a file(name) rather than a set of
> file-attributes.

I didn't realise Lars was proposing accessors on the return value of
file-attributes, which looks kinda cumbersome.

Leo




^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 17:08     ` Drew Adams
@ 2012-04-10  1:47       ` Stefan Monnier
  2012-04-10 10:58         ` Thien-Thi Nguyen
  0 siblings, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2012-04-10  1:47 UTC (permalink / raw)
  To: Drew Adams; +Cc: 'Leo', emacs-devel

> Yes, and `file-attribute-size' gives the impression that it is about
> the size of a file attribute.  And so on.

Yes, that is a bit annoying, although it does correspond to "classic"
Elisp naming.  I personally prefer Scheme style "file-attribute->size".


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 16:52         ` Ted Zlatanov
@ 2012-04-10  1:48           ` Stefan Monnier
  2012-04-11  1:55           ` Miles Bader
  1 sibling, 0 replies; 22+ messages in thread
From: Stefan Monnier @ 2012-04-10  1:48 UTC (permalink / raw)
  To: emacs-devel

>>> (file-attributes FILE nil 'size 'mtime)
SM> Too complex.
> ...as opposed to `(nth 7)' or a separate function for each attribute?
> Seriously?

Yes.


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 17:35   ` Lars Magne Ingebrigtsen
@ 2012-04-10  1:50     ` Stefan Monnier
  2012-04-10  2:00       ` Lars Magne Ingebrigtsen
  0 siblings, 1 reply; 22+ messages in thread
From: Stefan Monnier @ 2012-04-10  1:50 UTC (permalink / raw)
  To: emacs-devel

> (defun file-attribute-size (elem)
>   ...
>   (if (stringp file)
>       (nth 7 (file-attributes elem))
>     (nth 7 elem)))

You mean

  (nth 7 (if (stringp file) ...))

> What do you think?

I'm not sure I like it.  But if we do go this route, then we could name
it `file-size'.


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-10  1:50     ` Stefan Monnier
@ 2012-04-10  2:00       ` Lars Magne Ingebrigtsen
  2012-04-10 12:56         ` Stefan Monnier
  0 siblings, 1 reply; 22+ messages in thread
From: Lars Magne Ingebrigtsen @ 2012-04-10  2:00 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> You mean
>
>   (nth 7 (if (stringp file) ...))

Yes.  :-)

>> What do you think?
>
> I'm not sure I like it.

Yeah, me neither...

> But if we do go this route, then we could name it `file-size'.

For `file-size' in particular, that would make sense, but for
`file-modtime' and `file-uid' (etc) it seems a bit...  odd...  to put
these things that far "up" in the naming hierarchy.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-10  1:47       ` Stefan Monnier
@ 2012-04-10 10:58         ` Thien-Thi Nguyen
  2012-04-10 12:52           ` Stefan Monnier
  0 siblings, 1 reply; 22+ messages in thread
From: Thien-Thi Nguyen @ 2012-04-10 10:58 UTC (permalink / raw)
  To: emacs-devel

() Stefan Monnier <monnier@iro.umontreal.ca>
() Mon, 09 Apr 2012 21:47:41 -0400

   Yes, that is a bit annoying, although it does correspond to "classic"
   Elisp naming.  I personally prefer Scheme style "file-attribute->size".

In any case the container is not a single attribute but a set, so to
be excruciatingly correct, the name ought to be ‘file-attributes-size’
(or ‘file-attributes->size’), which is even less pleasant.  :-/



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-10 10:58         ` Thien-Thi Nguyen
@ 2012-04-10 12:52           ` Stefan Monnier
  0 siblings, 0 replies; 22+ messages in thread
From: Stefan Monnier @ 2012-04-10 12:52 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: emacs-devel

>    Yes, that is a bit annoying, although it does correspond to "classic"
>    Elisp naming.  I personally prefer Scheme style "file-attribute->size".
> In any case the container is not a single attribute but a set, so to
> be excruciatingly correct, the name ought to be ‘file-attributes-size’
> (or ‘file-attributes->size’),

Yes, sorry, that was a typo.

> which is even less pleasant.  :-/

I don't find it noticeably worse.


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-10  2:00       ` Lars Magne Ingebrigtsen
@ 2012-04-10 12:56         ` Stefan Monnier
  0 siblings, 0 replies; 22+ messages in thread
From: Stefan Monnier @ 2012-04-10 12:56 UTC (permalink / raw)
  To: Lars Magne Ingebrigtsen; +Cc: emacs-devel

BTW, another approach altogether is to represent file-attributes a bit
differently, e.g. as something like (<type> . <values>), where <type>
(which you could think of as a class if you like OO) describes which
fields exist and at which position, so with a few changes to `get' we
could use (get (file-attributes <file>) 'size).


        Stefan



^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: `file-attribute' accessors
  2012-04-09 16:52         ` Ted Zlatanov
  2012-04-10  1:48           ` Stefan Monnier
@ 2012-04-11  1:55           ` Miles Bader
  1 sibling, 0 replies; 22+ messages in thread
From: Miles Bader @ 2012-04-11  1:55 UTC (permalink / raw)
  To: emacs-devel

Ted Zlatanov <tzz@lifelogs.com> writes:
> SM> Too complex.
>
> ...as opposed to `(nth 7)' or a separate function for each attribute?
> Seriously?

Trivial accessor functions/macros are _really_ simple, both for humans
and for compilers...

-miles

-- 
古池や 蛙飛び込む 水の音 [松尾芭蕉]



^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2012-04-11  1:55 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-04-09 13:56 `file-attribute' accessors Lars Magne Ingebrigtsen
2012-04-09 14:19 ` Thierry Volpiatto
2012-04-09 14:40   ` Lars Magne Ingebrigtsen
2012-04-09 14:53     ` Thierry Volpiatto
2012-04-09 15:46     ` Ted Zlatanov
2012-04-09 16:29       ` Lars Magne Ingebrigtsen
2012-04-09 16:32       ` Stefan Monnier
2012-04-09 16:52         ` Ted Zlatanov
2012-04-10  1:48           ` Stefan Monnier
2012-04-11  1:55           ` Miles Bader
2012-04-09 15:32 ` Leo
2012-04-09 16:31   ` Stefan Monnier
2012-04-09 17:08     ` Drew Adams
2012-04-10  1:47       ` Stefan Monnier
2012-04-10 10:58         ` Thien-Thi Nguyen
2012-04-10 12:52           ` Stefan Monnier
2012-04-09 17:57     ` Leo
2012-04-09 16:33 ` Stefan Monnier
2012-04-09 17:35   ` Lars Magne Ingebrigtsen
2012-04-10  1:50     ` Stefan Monnier
2012-04-10  2:00       ` Lars Magne Ingebrigtsen
2012-04-10 12:56         ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).