Templates and stringof...
Era Scarecrow
rtcvb32 at yahoo.com
Fri Aug 3 20:07:20 PDT 2012
This is moved/copied from the D.learning group. I need opinions
on the matter, and thoughts perhaps from Walter or Andrei.
Here are the highlights
---
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'?
On Friday, 3 August 2012 at 21:19:08 UTC, David Nadlinger wrote:
> 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.
On Friday, 3 August 2012 at 22:47:52 UTC, David Nadlinger wrote:
> 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?
I would think, the exact 'text' of the way the variable was
called, in my example it would be "inum.i", and since it would be
at the local level (where the mixin was called) it should have
access to everything that it did at that time.
On Friday, 3 August 2012 at 22:50:54 UTC, David Nadlinger wrote:
> 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.
Then doesn't it seem like we're missing a potentially important
piece of the puzzle for mixins and templates? very likely my
modified template will include you including the same variable
twice, but if someone gets lazy then it may not work.
True I can add asserts as part of the output code, but as
mentioned hopefully the constraints could do that work, plus
adding extra checks as part of a mixin seems a little...
excessive, dirty and ugly.
Let's assume we use 'localStringof' or 'callingStringof' and
that it returns the string of how the variable is
called/referenced; Give me some examples how it would be abused
or used wrongly? Aliased variables are pretty much perfect
forwarded in templates (Unless my understanding is off) so they
would carry that information forward.
More information about the Digitalmars-d
mailing list