Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19332

Re: What about namespaces ?

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: What about namespaces ?
Date 2012-04-04 18:43 +0000
Organization Aioe.org NNTP Server
Message-ID <20120404103518.179@kylheku.com> (permalink)
References <jleflk$kv0$1@speranza.aioe.org> <namespace-20120403212713@ram.dialup.fu-berlin.de> <0.6206733be66982d9ac80.20120404043729BST.87vclggoau.fsf@bsb.me.uk>

Show all headers | View raw


On 2012-04-04, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
>> Rui Maciel <rui.maciel@gmail.com> writes:
>>>So, with this in mind, what are your thoughts on the idea of introducing 
>>>namespaces to the C programming language?  Would it be a bad idea? 
>>
>>   One can collect all names of a library in a struct. The struct is then
>>   published by a pointer. So there only is a single external identifier
>>   necessary per lib, which can be edited in the case of a clash.
>
> That does not work for types and constants.

It doesn't matter because these things do not have linkage. Clashes
among names that don't have any sort of pervasive scope such as external
linkage can be solved with a local renaming.

E.g. two library header files define "struct foo". No problem.

  #define foo abc_foo
  #include "abclib.h"
  #undef foo

  #define foo xyz_foo
  #include "xyzlib.h"
  #undef foo

Now abclib's struct foo is called struct abc_foo.

C++ is worse in this regard because class names have linkage.

This is why C++ needs namespaces: so you can put classes in them.
If two libraries in a C program make "typedef struct stack" it's not
as bad as two "class stack" in the same C++ program.

As far as external name clashes go, that can be solved by technologies
outside of C, such as symbol export controls on shared libraries.
That is to say, you can have external names in a shared library, referenced
among its modules, which are not exposed outside of the shared library.  The
external API is often a lot smaller than the sum total of all API's contained
in the component, internal plus external.

Even a short prefix on names goes a long way. A random three-letter prefix
gives you 26**3 possible namespaces.

Higher level languages with bindings to C libraries can hide that cruft, and
that's pretty much what matters nowadays. Nobody cares about programming at
large in the C language with nice namespaces.

Ultimately, a namespace feature would just translate into some mangling
that the linker level.
What might be nice would be to treat underscores as virtual namespace
delimiters. If I want to have a bunch of symbols in a scope that all begin with
foo_ it would be nice to have:

  prefix foo_ {
    typedef int x; // this is really foo_x 
    struct stack { // this is really struct foo_stack
      int x;       // this is just x
      /* ... */
    } s; // this is really foo_s
  }

  // pull foo_bar, foo_xyzzy and foo_x into this scope
  // under the short aliases bar, xyzzy and x.
  prefix foo_ bar, xyzzy, x;

This is just a dumb text processing hack that doesn't pretend it's anything
more (unlike C++ namespaces which are a text processing hack which pretends to
be more).

In the preprocessor:

  #prefix FOO_
  #define X Y  // actually defines FOO_X Y
  #endfix
  #addpfx FOO_ A B C // #define-s A B C to be aliases for FOO_A, FOO_B, FOO_C.

Here, if FOO_B is a function-like macro, then B becomes a function-like
macro also. Design choices: it can have a life of its own (enduring after
#undef FOO_B) or it could be linked to FOO_B such that it follows redefinitions
of FOO_B, and disappears when #undef FOO_B is performed).

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 10:25 +0100
  Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-03 11:20 +0100
    Re: What about namespaces ? Ian Collins <ian-news@hotmail.com> - 2012-04-03 22:29 +1200
      Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-03 12:38 +0100
    Re: What about namespaces ? Keith Thompson <kst-u@mib.org> - 2012-04-03 12:19 -0700
      Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-03 20:57 +0100
  Re: What about namespaces ? gordonb.9sh3w@burditt.org (Gordon Burditt) - 2012-04-03 06:23 -0500
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 19:54 +0100
      Re: What about namespaces ? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-04-03 12:18 -0700
        Re: What about namespaces ? jgharston <jgh@arcade.demon.co.uk> - 2012-04-03 13:09 -0700
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 19:54 +0100
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 19:54 +0100
  Re: What about namespaces ? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-03 06:49 -0700
  Re: What about namespaces ? Robert Wessel <robertwessel2@yahoo.com> - 2012-04-03 11:10 -0500
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 20:12 +0100
    Re: What about namespaces ? Ian Collins <ian-news@hotmail.com> - 2012-04-04 07:20 +1200
  Re: What about namespaces ? Fritz Wuehler <fritz@spamexpire-201204.rodent.frell.theremailer.net> - 2012-04-03 20:58 +0200
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 21:05 +0100
      Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-03 21:33 +0100
  Re: What about namespaces ? William Ahern <william@wilbur.25thandClement.com> - 2012-04-03 12:05 -0700
    Re: What about namespaces ? James Kuyper <jameskuyper@verizon.net> - 2012-04-03 15:50 -0400
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-03 21:27 +0100
      Re: What about namespaces ? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-04-03 14:40 -0700
    Re: What about namespaces ? jgk@panix.com (Joe keane) - 2012-04-11 20:56 +0000
      Re: What about namespaces ? Ben Pfaff <blp@cs.stanford.edu> - 2012-04-11 14:46 -0700
        Re: What about namespaces ? jgk@panix.com (Joe keane) - 2012-04-11 23:03 +0000
        Re: What about namespaces ? jgk@panix.com (Joe keane) - 2012-04-13 00:40 +0000
          Re: What about namespaces ? Keith Thompson <kst-u@mib.org> - 2012-04-12 18:56 -0700
            Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-13 13:20 +0100
              Re: What about namespaces ? Keith Thompson <kst-u@mib.org> - 2012-04-13 13:43 -0700
            Re: What about namespaces ? jgk@panix.com (Joe keane) - 2012-04-15 16:46 +0000
              Re: What about namespaces ? Martin Ambuhl <mambuhl@earthlink.net> - 2012-04-21 04:38 -0400
                Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-21 16:44 +0100
      Re: What about namespaces ? "BartC" <bc@freeuk.com> - 2012-04-11 23:01 +0100
        Re: What about namespaces ? jgk@panix.com (Joe keane) - 2012-04-13 21:04 +0000
      Re: What about namespaces ? lawrence.jones@siemens.com - 2012-04-11 22:09 -0400
        Re: What about namespaces ? Nomen Nescio <nobody@dizum.com> - 2012-04-12 10:55 +0200
  Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-04 00:17 +0100
    Re: What about namespaces ? William Ahern <william@wilbur.25thandClement.com> - 2012-04-03 16:44 -0700
      Re: What about namespaces ? nick_keighley_nospam@hotmail.com - 2012-04-04 00:49 -0700
  Re: What about namespaces ? Ian Collins <ian-news@hotmail.com> - 2012-04-04 14:54 +1200
    Re: What about namespaces ? Ian Collins <ian-news@hotmail.com> - 2012-04-04 15:43 +1200
      Re: What about namespaces ? Ian Collins <ian-news@hotmail.com> - 2012-04-04 16:06 +1200
  Re: What about namespaces ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-04-04 04:37 +0100
    Re: What about namespaces ? Kaz Kylheku <kaz@kylheku.com> - 2012-04-04 18:43 +0000
      Re: What about namespaces ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-04-05 01:43 +0100
  Re: What about namespaces ? "io_x" <a@b.c.invalid> - 2012-04-04 09:32 +0200
  Re: What about namespaces ? Tim Rentsch <txr@alumni.caltech.edu> - 2012-04-05 11:22 -0700
    Re: What about namespaces ? Rui Maciel <rui.maciel@gmail.com> - 2012-04-05 21:04 +0100
    Re: What about namespaces ? Kaz Kylheku <kaz@kylheku.com> - 2012-04-06 01:10 +0000

csiph-web