[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ptrdiff_t misuse

From: Eli Zaretskii
Subject: Re: ptrdiff_t misuse
Date: Fri, 06 Jul 2012 14:01:45 +0300

> From: "Stephen J. Turnbull" <address@hidden>
> Date: Fri, 06 Jul 2012 19:19:57 +0900
> Cc: Paul Eggert <address@hidden>, address@hidden,
>       address@hidden, address@hidden
> ptrdiff_t expresses exactly the type required by the hardware.

For pointer arithmetics, yes.  Which is not what we normally do with
buffer and string positions: we use them as normal integral types.

Compare that with typical declarations of array indices in C programs:
you'll rarely if ever see them declared as ptrdiff_t.

>  > since ptrdiff_t is a rarely-used type,
> EMACS_INT of course is even more rarely-used. ;-)

Not for someone who hacks Emacs sources.  It's like Lisp_Object: you
get used to it very quickly and use it naturally after that.

>  > and thus I expect many programmers to be unfamiliar with it.
> So teach them, just as you have to teach them EMACS_INT.  The type
> name is mnemonic, as are the variables it describes.

The type name actively resists that.  The "ptr" part is one problem,
the "diff" part is another.  None of them is related to the typical
usage of the positional values.

> [EMACS_POS] doesn't tell me anything about the actual type, or why
> it needs to be different from EMACS_INT.

Why would you care?  I don't expect J.R. Hacker to care about that,
except if she is a type junkie, in which case she just has to M-. on
the type name and see the guts.  Otherwise, all she needs to remember
is that it's "the type used for Emacs buffer/string positions".

> I think it will be very easy for people to
> confound EMACS_INT with EMACS_POS, even if they're read the typedef,
> because in Lisp both kinds of value are represented by the same type.

It is easy enough to explain that EMACS_POS is used for Lisp integers
that express buffer and string positions, and EMACS_INT for any other
Lisp integer values.

> The real problem is that lazy programmers will frequently use a
> generic type (such as "int") for variables, because they can't
> remember the right type.

I expect them to see the common practice in Emacs sources and follow
suit.  If not, they will get comments on their code to that effect and
learn fast enough.

> To me, "EMACS_POS" is only marginally more memorable than
> "ptrdiff_t" for this purpose

I didn't have you (or myself) in mind when I suggested EMACS_POS ;-)
I worry about newcomers, and would like to make their learning curve
in the matter to be as un-steep as possible.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]