Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 14, 2006 Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Is there any template means by which I can create a string representation from an identifier name at compile time? I.e. given the identifier name i, I'd like a template that can create a string "i". Thanks in advance for any ideas. Regards, Garett |
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | Garett Bass wrote:
> Is there any template means by which I can create a string representation from an identifier name at compile time? I.e. given the identifier name i, I'd like a template that can create a string "i". Thanks in advance for any ideas.
>
> Regards,
> Garett
I asked for this sort of feature in another post last year. The compiler currently does not have this ability, although I think it's important and useful. If the D infrastructure is to be capable of replacing the preprocessors of languages like C/C++, it will need to offer some capability of this sort. While exceedingly elegant compared to preprocessor macros, version control and templates just don't provide comprehensive functionality yet (in this particular area, anyway; templates in D are a wonderful simplification of C++ templates obfuscation).
-JJR
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | Garett Bass wrote:
> Is there any template means by which I can create a string representation from an identifier name at compile time? I.e. given the identifier name i, I'd like a template that can create a string "i". Thanks in advance for any ideas.
>
> Regards,
> Garett
Well, I have an idea.
You can get a string representation of class names, i.e., for a given class A, you can do this:
### code:
import std.stdio;
class A
{
}
int main()
{
A a = new A();
char[] str = a.classinfo.name;
writefln(str);
return 0;
}
### /code
will print A to the screen.
I don't know if this can exploited to create a work around for "stringizing" an identifier.
Templates are not my area ..
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote:
> Garett Bass wrote:
>> Is there any template means by which I can create a string representation from an identifier name at compile time? I.e. given the identifier name i, I'd like a template that can create a string "i". Thanks in advance for any ideas.
>>
>> Regards,
>> Garett
>
> Well, I have an idea.
> You can get a string representation of class names, i.e., for a given class A, you can do this:
>
> ### code:
> import std.stdio;
>
> class A
> {
> }
>
> int main()
> {
> A a = new A();
> char[] str = a.classinfo.name;
> writefln(str);
>
> return 0; }
> ### /code
>
> will print A to the screen.
>
> I don't know if this can exploited to create a work around for "stringizing" an identifier.
> Templates are not my area ..
>
The classinfo.name property is a feature for classes only (as the property name implies). You can't do anything like that for functions, methods, or variables.
-JJR
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | John Reimer wrote:
> Hasan Aljudy wrote:
>> Garett Bass wrote:
>>> Is there any template means by which I can create a string representation from an identifier name at compile time? I.e. given the identifier name i, I'd like a template that can create a string "i". Thanks in advance for any ideas.
>>>
>>> Regards,
>>> Garett
>>
>> Well, I have an idea.
>> You can get a string representation of class names, i.e., for a given class A, you can do this:
>>
>> ### code:
>> import std.stdio;
>>
>> class A
>> {
>> }
>>
>> int main()
>> {
>> A a = new A();
>> char[] str = a.classinfo.name;
>> writefln(str);
>>
>> return 0; }
>> ### /code
>>
>> will print A to the screen.
>>
>> I don't know if this can exploited to create a work around for "stringizing" an identifier.
>> Templates are not my area ..
>>
>
> The classinfo.name property is a feature for classes only (as the property name implies). You can't do anything like that for functions, methods, or variables.
>
> -JJR
Although... you /can/ get the "type" name of a variable converted to a string from its id.
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | John Reimer wrote:
> Hasan Aljudy wrote:
>
>> Garett Bass wrote:
>>
>>> Is there any template means by which I can create a string representation from an identifier name at compile time? I.e. given the identifier name i, I'd like a template that can create a string "i". Thanks in advance for any ideas.
>>>
>>> Regards,
>>> Garett
>>
>>
>> Well, I have an idea.
>> You can get a string representation of class names, i.e., for a given class A, you can do this:
>>
>> ### code:
>> import std.stdio;
>>
>> class A
>> {
>> }
>>
>> int main()
>> {
>> A a = new A();
>> char[] str = a.classinfo.name;
>> writefln(str);
>>
>> return 0; }
>> ### /code
>>
>> will print A to the screen.
>>
>> I don't know if this can exploited to create a work around for "stringizing" an identifier.
>> Templates are not my area ..
>>
>
> The classinfo.name property is a feature for classes only (as the property name implies). You can't do anything like that for functions, methods, or variables.
>
> -JJR
I was thinking more of creating a class inside the template, where the class name will be the supplied parameter (identifier).
I guess that isn't possible .. is it?
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | >
>I was thinking more of creating a class inside the template, where the
>class name will be the supplied parameter (identifier).
>I guess that isn't possible .. is it?
Do you mean like reflection? We're not quite there yet, but there is considerable demand for such a feature: you're not alone.
- EricAnderton at yahoo
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | pragma wrote:
>>I was thinking more of creating a class inside the template, where the class name will be the supplied parameter (identifier).
>>I guess that isn't possible .. is it?
>
>
> Do you mean like reflection? We're not quite there yet, but there is
> considerable demand for such a feature: you're not alone.
>
> - EricAnderton at yahoo
Not really, I was thinking in terms of template .. i.e.
##
template createclass( T )
{
class T
{
}
}
...
mixin createclass!( ANewClassName ); //create a class called ANewClassName
##
but I discovered that it's not possible in D.
|
January 14, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote:
> pragma wrote:
>>>I was thinking more of creating a class inside the template, where the
>>>class name will be the supplied parameter (identifier).
>>>I guess that isn't possible .. is it?
>>
>>
>> Do you mean like reflection? We're not quite there yet, but there is considerable demand for such a feature: you're not alone.
>>
>> - EricAnderton at yahoo
>
> Not really, I was thinking in terms of template .. i.e.
>
> ##
> template createclass( T )
> {
> class T
> {
> }
> }
>
> ...
> mixin createclass!( ANewClassName ); //create a class called ANewClassName
> ##
>
> but I discovered that it's not possible in D.
I'm not really into templates yet, but have you tried alias parameters for that?
Lars Ivar Igesund
|
January 16, 2006 Re: Convert Identifier to String at compile-time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote: > pragma wrote: > >>> I was thinking more of creating a class inside the template, where the class name will be the supplied parameter (identifier). >>> I guess that isn't possible .. is it? >> >> >> >> Do you mean like reflection? We're not quite there yet, but there is >> considerable demand for such a feature: you're not alone. >> >> - EricAnderton at yahoo > > > Not really, I was thinking in terms of template .. i.e. > > ## > template createclass( T ) > { > class T > { > } > } > > ... > mixin createclass!( ANewClassName ); //create a class called ANewClassName > ## > > but I discovered that it's not possible in D. In general it's not possible. There's an interesting case where it is possible, but you have to do it manually: template createclass(char [] name) { static if (name[0]=='a') { class a { } else static if (name[0]=='b') { class b { } else static if (name[0]=='c') { class c { } } else static assert(0); // name not supported } mixin createclass!("a"); Of course, this is not much use. But it is an argument that a more general facility could be provided in the language without causing new problems. If the next line of code was a.somefunc(); an intellisense IDE is going to have a lot of trouble making sense of a. Since this problem already exists, it should reduce objection to the following proposal: I'd like to see the definition of Identifier changed to: Identifier: IdentifierStart IdentifierStart IdentifierChars identifier(StringLiteral) so that whereever you write abc you could also write identifier("abc") During the semantic pass, whenever identifier( _stringexpr_ ) is encountered, it would be replaced with _stringexpr_. In your case, it would let you write class identifier("ANewClassName") { } identifier("ANewClassName") identifier("x") = new identifier("ANewClassName"); Which would be completely useless, except that D provides compile-time string processing, so that the string literals could be created from constant strings. We'd have the equivalent of the C/C++ 'stringize' preprocessor operator, but fully integrated into the template system, and without wrecking the lexical and syntactic passes. Just need a 'tokenize' operator -- eg qualifiednameof(Identifier) eg const char [] y = qualifiednameof(x); static assert( y == "somemodule.SomeClass.someFunc.x"); This would mean that D templates would finally encompass all the functionality of the C++ preprocessor. A future step would be to add const char [][] membersof(Identifier) returning an array of all the names which were declared in the scope of Identifier. (although, this last one would not be useful until D gets array literals). Then we'd have full compile-time reflection, and the ability to dump arbitrary code into the optimiser based on the results. Undoubtedly far too much rope. :-) |
Copyright © 1999-2021 by the D Language Foundation