On Friday, 16 June 2017 at 03:57:17 UTC, Adam D. Ruppe wrote:
On Friday, 16 June 2017 at 03:26:28 UTC, Jonathan Marler wrote:
If you have a better idea on how to implement the bitfields template that would be great.

The real WTF is that it returns a string in the first place. It should return a struct.

Here, take a look at this:

-------------
/++
        Type, "name", 4 /* size_in_bits */
        // repeat
+/
mixin template bitfields(T...) {
        mixin((function() {
                import std.format;
                string code;
                code = "struct {";

                string getName(size_t idx)() { return T[idx]; }

                foreach(i, t; T) {
                        static if(i % 3 == 0) {
// I'm just doing a fake getter here to demo the // technique, you can do setter the same way
                                code ~= format("T[%d] %s() {
return cast(typeof(return)) T[%d];
                                }\n", i + 0, T[i + 1], i + 2);
                        }
                }

                code ~= "}";
                return code;
        })());
}

import std.typecons;

struct HasBitfields {
        mixin bitfields!(
                bool, "bool_thing", 1,
                int, "int_thing", 4,
                Flag!"CustomFlag", "custom_flag", 1,
        );
}

void main() {
        HasBitfields bf;
        assert(bf.bool_thing == 1);
        assert(bf.int_thing == 4);

        import std.stdio;
        foreach(n; __traits(allMembers, HasBitfields))
// this is the only time you should ever use stringof - printing // basic info to the user that is not meant to be seriously parsed;
                // it is a debugging aid.
writeln(typeof(__traits(getMember, HasBitfields, n)).stringof, " ", n);
}
--------------


I didn't actually implement the getter/setter functions correctly, but adapting the current code to use this superior technique shouldn't be too hard.

Of course, it isn't 100% compatible on the user side... but, as you can see in the example, all types just work with no name troubles.

Notice this code here:

  code ~= format("T[%d] %s() {
    return cast(typeof(return)) T[%d];
  }\n", i + 0, T[i + 1], i + 2);


There's no `.stringof` in there. Instead, I just use `T[x]` to represent the type - the local alias that was passed in by the user.

This has potential. I'll work with this to see if I can implement a better version of bitfields. Since it is called slightly differently phobos inclusion is more complex, requiring a deprecation process or a different name, however, even if it's not in phobos I can use it in my own projects which is utlimately what I need. I do like this API better since a "mixin template" more clearly indicates what bifields is doing, mixing in some fields.

I see you put a comment in your example,

// this is the only time you should ever use stringof - printing // basic info to the user that is not meant to be seriously parsed;
        // it is a debugging aid.

Obviously T.stringof falls very short when used for mixing in type names. However the real question I posed was how to implement a function that can be used for this. I showed how fullyQualifiedName doesn't work either, but I think an implementation of relativeQualifiedName would work. I see your argument that such a template shouldn't be necessary, but that assumes that there is no useful case for mixing in types names. I'm not convinced that this is the case but I haven't given you a counter-example so I'll leave it for now. In the meantime thanks for a potential solution to the bitfields problem.




Reply via email to