Thread overview
Feature Request: Keyword as identifier.
Sep 29, 2006
icee
Sep 29, 2006
Frits van Bommel
Sep 29, 2006
Charlie
Sep 29, 2006
Stewart Gordon
Sep 30, 2006
icee
Oct 02, 2006
Don Clugston
Oct 02, 2006
Stewart Gordon
Oct 02, 2006
J Duncan
September 29, 2006
In C# we can use a keyword as a identifier by prefixing @

Why?
suppose we are using a extern C lib, the library exports a function as "int
version()",
so we write:
 extern(C) int version();
this will obviously fail to compile, cos' version is a D keyword, (but not
the C keyword.)
so we have no methods except modify the C code and re-compile it.



September 29, 2006
icee wrote:
> In C# we can use a keyword as a identifier by prefixing @
> 
> Why?
> suppose we are using a extern C lib, the library exports a function as "int version()",
> so we write:
>  extern(C) int version();
> this will obviously fail to compile, cos' version is a D keyword, (but not the C keyword.)
> so we have no methods except modify the C code and re-compile it.

Actually, there's another option: create a C wrapper around it with another name.

	#include "whatever/version.h"

	int C_version() { return version(); }

Doesn't help much perhaps, but doesn't require recompilation. Which is good if you don't have the source of the lib to do so, for instance.
Plus this might compile a lot faster than the lib ;).
September 29, 2006
I think you can also alias it in the .def file ?

Frits van Bommel wrote:
> icee wrote:
>> In C# we can use a keyword as a identifier by prefixing @
>>
>> Why?
>> suppose we are using a extern C lib, the library exports a function as "int version()",
>> so we write:
>>  extern(C) int version();
>> this will obviously fail to compile, cos' version is a D keyword, (but not the C keyword.)
>> so we have no methods except modify the C code and re-compile it.
> 
> Actually, there's another option: create a C wrapper around it with another name.
> 
>     #include "whatever/version.h"
> 
>     int C_version() { return version(); }
> 
> Doesn't help much perhaps, but doesn't require recompilation. Which is good if you don't have the source of the lib to do so, for instance.
> Plus this might compile a lot faster than the lib ;).
September 29, 2006
icee wrote:
> In C# we can use a keyword as a identifier by prefixing @
> 
> Why?
> suppose we are using a extern C lib, the library exports a function as "int version()",
> so we write:
>  extern(C) int version();
> this will obviously fail to compile, cos' version is a D keyword, (but not the C keyword.)

"cos'"?

> so we have no methods except modify the C code and re-compile it.

See also my proposal for an alternative solution:

http://tinyurl.com/gsan2

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
September 30, 2006
Fortunately i don't have to handle this in the real cases right now.
Thanks for all the suggestions.
Wrapper like "C_version" maybe is a little ugly,:) and not convenient
enough.
Your proposal about extren(...) may be a good solution, and focus on the
foreign symbol name.

What i'm requesting is the general ability to use any reserved word as
identifier in D codes,
Because of the complexity of D language, it require more words to be
reserved. this may break the programmer's naming rule and convenience. I
think allowing keyword as id may improve the user experience.
@version? $version? @(...any)? or some what ?

> "cos'"?

I mean "because" Coz My poor and lazy En;)


October 02, 2006
icee wrote:
> In C# we can use a keyword as a identifier by prefixing @
> 
> Why?
> suppose we are using a extern C lib, the library exports a function as "int version()",
> so we write:
>  extern(C) int version();
> this will obviously fail to compile, cos' version is a D keyword, (but not the C keyword.)
> so we have no methods except modify the C code and re-compile it.

I proposed a keyword called 'identifier':

extern(C) int identifier("version")();

As well as solving this problem, it would add the most powerful remaining C preprocessor feature (token pasting) into the D template system. Serious magic. Although D has no shortage of magic already <g>.
October 02, 2006
Don Clugston wrote:
<snip>
> I proposed a keyword called 'identifier':
> 
> extern(C) int identifier("version")();
> 
> As well as solving this problem, it would add the most powerful remaining C preprocessor feature (token pasting) into the D template system. Serious magic. Although D has no shortage of magic already <g>.

What practical uses are there for token pasting in D?

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
October 02, 2006

Stewart Gordon wrote:
> Don Clugston wrote:
> <snip>
> 
>> I proposed a keyword called 'identifier':
>>
>> extern(C) int identifier("version")();
>>
>> As well as solving this problem, it would add the most powerful remaining C preprocessor feature (token pasting) into the D template system. Serious magic. Although D has no shortage of magic already <g>.
> 
> 
> What practical uses are there for token pasting in D?
> 
> Stewart.
> 

I can think of many uses of token pasting, the most obvious to allow a mixin to programatically create identifiers (which would be a boon to the severely limited mixins). combine it with reflection and you have all sorts of fun options with generating code