git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Witten <mfwitten@gmail.com>
To: Jared Hance <jaredhance@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: [RFC] struct *_struct
Date: Thu, 05 Aug 2010 20:57:13 -0700 (PDT)	[thread overview]
Message-ID: <4c5b8819.4f3fdc0a.14ad.22b1@mx.google.com> (raw)
In-Reply-To: <20100805224321.GA22430@localhost.localdomain>

On Wed, Aug 4, 2010 at 14:24, Junio C Hamano <gitster@pobox.com> wrote:
>>> I hate... "typedef foo struct foo"

On Thu, Aug 05, 2010 at 11:20:14AM -0500, Michael Witten wrote:
>> How come?

On Thu, Aug 5, 2010 at 17:43, Jared Hance <jaredhance@gmail.com> wrote:
> In my opinion, it creates ambiguity. If I have
>
>    typedef struct foo foo;
>
> And I have "foo" used in a code snippet, it is much less easier to see
> if foo is being used in the type context or if its an instance, since
> I like to do
>
>    struct foo foo;
>
> which reads much less well as:
>
>    foo foo;
>
>
> Its also much less easier to grep though to find all the places the
> type is used. If I do
>
>    $ git grep "foo"
>
> I will end up with the instances and the struct type. whereas I can do
>
>    $ git grep "struct foo"
>
> to find (most|all) of the types, depending on whether the code uses
> decent practices (there shouldn't be a second space between struct and
> foo, or a newline between them).
>
> I could also use a similar regular expression to find all the
> instances (ie, all the instances of foo that aren't prefixed with
> struct).

Those are valid points, but I'm not sure they have a practical basis;
your problems are largely solved by capitalization conventions
(which essentially provide shorter replacements for `struct '):

    typedef struct { /* ... */ } Foo;
    Foo foo;

Unfortunately, such conventions don't enjoy the benefit of semantic
protection. However, language-aware source navigation tools (like ctags)
should be able to solve that problem and are probably more efficient
in navigation time than grepping.

Moreover, the form:

    foo foo;

is probably not that problematic in practice; it's presence is likely
to be short lived for 2 reasons:

    * Subjectively : everyone thinks it looks awful.
    * Objectively  : It's technically constrained.

The typedef declaration:

    typedef /*type*/ foo;

introduces the typedef name `foo' into the `ordinary identifier'
name space; consequently, the declaration:

    foo foo;

cannot even occur in the same scope as the typdef, and when
it does occur in an inner scope, it hides the original typdef
name `foo' for all subsequent inner scopes:

    typedef struct {char x;} foo;

    foo foo;         // error: attempt to redeclare `foo'.
    foo a;

    int main()
    {

      foo foo;       // OK; hide typedef name with variable `foo'
      foo b;         // error: `foo' is not a type.

      {

        foo c;       // error: `foo' is not a type.

        typedef struct {char x;} foo;   // OK; hide variable `foo'

        foo foo;     // error: attempt to redeclare `foo'
        foo d;

        d = a;       // error: anonymous structs are always different types.

        {
          foo foo;   // OK; hide typedef name with variable `foo'
          d = foo;   // OK; same type
          foo e;     // error: `foo' is not a type.
        }

        {
          foo foo;   // OK; hide typedef name with variable `foo'
          d = foo;   // OK; same type
          foo f;     // error: `foo' is not a type.
        }

      }

    }

Sincerely,
Michael Witten

  reply	other threads:[~2010-08-06  3:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-04 15:08 [RFC] struct *_struct Jared Hance
2010-08-04 19:24 ` Junio C Hamano
2010-08-04 21:38   ` [PATCH] Refactor structures in the form of *_struct Jared Hance
2010-08-05 16:20   ` [RFC] struct *_struct Michael Witten
2010-08-05 22:43     ` Jared Hance
2010-08-06  3:57       ` Michael Witten [this message]
2010-08-06 12:29         ` Jared Hance
2010-08-06  2:28     ` Miles Bader

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c5b8819.4f3fdc0a.14ad.22b1@mx.google.com \
    --to=mfwitten@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jaredhance@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).