View previous topic :: View next topic |
Author |
Message |
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Fri Oct 21, 2005 12:14 pm Post subject: Restricted names |
|
|
With the ability to declare things private at the module level I was hoping this could be avoided, but it recently occurred to me that there still may be problems with extern (C) callback functions. For example, std.exception includes these functions for internal use:
Code: | extern (C) void onAssert( char[] file, uint line );
extern (C) void onOutOfMemory();
extern (C) void onArrayBoundsError( char[] file, size_t line );
extern (C) void onSwitchError( char[] file, size_t line );
extern (C) void onInvalidUtfError( char[] msg, size_t idx ); |
Making these functions private (which they will be in the next release) doesn't help because the functions are still callable so long as the user declares them. And while I think it's unlikely that a user would create a function with one of these signatures for his own use, it's still not a chance I'd like take. How about reserving symbol names with a leading underscore for library use? Thus, the above functions would become:
Code: | extern (C) void _onAssert( char[] file, uint line );
extern (C) void _onOutOfMemory();
extern (C) void _onArrayBoundsError( char[] file, size_t line );
extern (C) void _onSwitchError( char[] file, size_t line );
extern (C) void _onInvalidUtfError( char[] msg, size_t idx ); |
The garbage collector declares a ton of callback functions for various things as well, so this list will grow in the not too distant future. |
|
Back to top |
|
|
pragma
Joined: 28 May 2004 Posts: 607 Location: Washington, DC
|
Posted: Fri Oct 21, 2005 12:25 pm Post subject: |
|
|
As long as its the low-level extern(C) stuff, that's okay by me. Better avoid those namespace collisions now rather than later. _________________ -- !Eric.t.Anderton at gmail |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Fri Oct 21, 2005 2:53 pm Post subject: |
|
|
I agree ~ reserving part of the namespace for the library is a good idea. You could use the prefix "ares" instead of an underscore, but the latter is traditional along with the __ variety (used in C libraries).
- Kris |
|
Back to top |
|
|
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Fri Oct 21, 2005 4:04 pm Post subject: |
|
|
The only restriction is that external symbol names can't collide with anything that may be linked in from a C library, so leading double underscore and underscore followed by a capital letter are definately out. DMD uses "_d_" for callbacks in Phobos, so perhaps Ares should follow suit. This is a strict enough convention that it's not likely to conflict with anything even if someone tries linking a D library into a C program. Another alternative would be to use a UniversalAlpha character as the prefix, though this might be a bit difficult to read. |
|
Back to top |
|
|
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Sat Oct 22, 2005 3:06 pm Post subject: |
|
|
See the general forum. I've settled on "_D_" for now, pending any criticism from Walter. So all extern (C) functions in Ares that are not intended to be user callable will be private and have this prefix. |
|
Back to top |
|
|
|