Jump to page: 1 2 3
Thread overview
Templates and stringof...
Aug 03, 2012
Era Scarecrow
Aug 03, 2012
David Nadlinger
Aug 03, 2012
Era Scarecrow
Aug 03, 2012
David Nadlinger
Aug 03, 2012
Andrej Mitrovic
Aug 03, 2012
Andrej Mitrovic
Aug 03, 2012
Era Scarecrow
Aug 03, 2012
David Nadlinger
Aug 03, 2012
Era Scarecrow
Aug 04, 2012
Philippe Sigaud
Aug 04, 2012
Era Scarecrow
Aug 04, 2012
David Nadlinger
Aug 04, 2012
Philippe Sigaud
Aug 04, 2012
David Nadlinger
Aug 04, 2012
Philippe Sigaud
Aug 04, 2012
Jonathan M Davis
Aug 04, 2012
David Nadlinger
Aug 03, 2012
Andrej Mitrovic
Aug 03, 2012
David Nadlinger
Aug 03, 2012
Era Scarecrow
Aug 04, 2012
Philippe Sigaud
August 03, 2012
 While working on bitfields code I've found a unique scenario that poses some annoyances when generating the code.


template XYZ(alias x) {
    enum XYZ = x.stringof ~ "=100;";
}

struct I { int i;}

I i_num;
int n;

mixin(XYZ!(i_num.i)); //cannot find variable i
mixin(XYZ!(n));

the mixins become:
i=100;
n=100;


 Now, how do I get the template's stringof to print out 'i_num.i' and not 'i'?
August 03, 2012
On Friday, 3 August 2012 at 21:02:22 UTC, Era Scarecrow wrote:
>  Now, how do I get the template's stringof to print out 'i_num.i' and not 'i'?

You don't. Using .stringof in conjunction with string mixins is The Wrong Thing (tm) in virtually all cases.

What do you want to achieve? Why can't you pass a string instead?

David


August 03, 2012
On Friday, 3 August 2012 at 21:19:08 UTC, David Nadlinger wrote:
> On Friday, 3 August 2012 at 21:02:22 UTC, Era Scarecrow wrote:
>> Now, how do I get the template's stringof to print out 'i_num.i' and not 'i'?
>
> You don't. Using .stringof in conjunction with string mixins is The Wrong Thing (tm) in virtually all cases.
>
> What do you want to achieve? Why can't you pass a string instead?

 Because a string doesn't hold it's type information for size checking.

 int = 4 bytes or 32 bits
 string = ????

 It also checks for unsuitable types like structs and floats; can't do that with strings.
August 03, 2012
On 8/3/12, Era Scarecrow <rtcvb32@yahoo.com> wrote:
>   Now, how do I get the template's stringof to print out 'i_num.i'
> and not 'i'?

Courtesy of Philippe Sigaud, slightly edited to my style:

/**
    Return the fully qualified name of a symbol.
    Implemented by Philippe Sigaud in the D Templates book.
    See https://github.com/PhilippeSigaud/D-templates-tutorial
*/
template ScopedName(alias a)
{
    // does it have a parent?
    static if (__traits(compiles, __traits(parent, a)))
    {
        // If yes, get the name and recurse
        enum ScopedName = ScopedName!(__traits (parent, a)) ~ "." ~ NameOf!(a);
    }
    else
    {
        // if not, it’s a module name. Stop there.
        enum ScopedName = NameOf!a;
    }
}

template NameOf(alias a)
{
    enum string NameOf = __traits(identifier, a);
}

template XYZ(alias x)
{
    enum XYZ = ScopedName!x ~ "=100;";
}
August 03, 2012
On 8/4/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 8/3/12, Era Scarecrow <rtcvb32@yahoo.com> wrote:
>>   Now, how do I get the template's stringof to print out 'i_num.i'
>> and not 'i'?
>  snip

Ahh crap, it doesn't return the *instance* name. Sorry!! Maybe there can be a fix though, I'll give it a try..
August 03, 2012
On Friday, 3 August 2012 at 22:18:25 UTC, Andrej Mitrovic wrote:
> On 8/4/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>> On 8/3/12, Era Scarecrow <rtcvb32@yahoo.com> wrote:
>>>   Now, how do I get the template's stringof to print out 'i_num.i'
>>> and not 'i'?
>>  snip
>
> Ahh crap, it doesn't return the *instance* name. Sorry!! Maybe there can be a fix though, I'll give it a try..

Mmmm worse comes to worse i can do a mixin, which generates the text into it's non-text verision and passes both to my template which then does it's appropriate checks.

 Seems like an ugly hack though (to get this done). Why not have another method of fullpathStringof or something similar? Then again if this is one of the few cases that could benefit from it, then maybe we should make it ugly so no one else will use it.
August 03, 2012
On Friday, 3 August 2012 at 21:33:35 UTC, Era Scarecrow wrote:
>  Because a string doesn't hold it's type information for size checking.
>
>  int = 4 bytes or 32 bits
>  string = ????

Just emit the check into the code you generate.

>  It also checks for unsuitable types like structs and floats; can't do that with strings.

Just emit the check into the code you generate.

David
August 03, 2012
On 8/4/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 8/3/12, Era Scarecrow <rtcvb32@yahoo.com> wrote:
>>   Now, how do I get the template's stringof to print out 'i_num.i'
>> and not 'i'?
>
> Courtesy of Philippe Sigaud, slightly edited to my style:

In fact, this should really be put into Phobos so everyone can benefit rather than implementing this internally as a private template of bitmanip.
August 03, 2012
On Friday, 3 August 2012 at 22:23:23 UTC, Era Scarecrow wrote:
>  Seems like an ugly hack though (to get this done). Why not have another method of fullpathStringof or something similar? Then again if this is one of the few cases that could benefit from it, then maybe we should make it ugly so no one else will use it.

This can't work in general. What should such a function return? The fully qualified name, i.e. including packages and modules? What is if the referred to entity is a nested function/local variable? What is if it is defined in a module which is not imported in the place where the string is mixed in?

Regarding Philippe Sigaud's stuff: He has done a lot of great work, but exactly this use of strings is why some of his template constructions are not usable in the real world. Again, using .stringof in conjunction with string mixins is almost never a good idea. Please trust me on this, I've done quite a bit of metaprogramming-heavy projects, and there are always ways around this, which are often more elegant as well.

David
August 03, 2012
On Friday, 3 August 2012 at 22:44:28 UTC, Andrej Mitrovic wrote:
> On 8/4/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>> Courtesy of Philippe Sigaud, slightly edited to my style:
>
> In fact, this should really be put into Phobos so everyone can benefit
> rather than implementing this internally as a private template of
> bitmanip.

No, it shouldn't be. There are almost no legit use cases for it, and having it in the library encourages abuse of string mixins/stringof in cases like this one.
« First   ‹ Prev
1 2 3