D-issapointed after using C# for a while.

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 11 19:59:08 PST 2016


On Saturday, November 12, 2016 03:30:58 Jerry via Digitalmars-d wrote:
> On Saturday, 12 November 2016 at 01:02:05 UTC, Jonathan M Davis
>
> wrote:
> > On Friday, November 11, 2016 23:32:11 Jerry via Digitalmars-d
> >
> > wrote:
> >> D doesn't have bitfields though? Phobos just has a template
> >> that emulates them by mixing in functions that do the bit
> >> shifting. Because it done through mixins it means you can't
> >> attach attributes to the fields. Which I am in need of.
> >> Haven't found a good workaround though.
> >
> > What attributes do you need that aren't there already? UDAs?
> > Looking at the code, the full set of appropriate built-in
> > attributes are on them already.
> >
> > - Jonathan M Davis
>
> Yah I need to associate some extra information with the bitfields
> using UDAs.

It would definitely be possible to make it so that std.bitmanip.bitfields
could handle UDAs, but I don't expect that it would be particularly fun to
implement. e.g. the example

struct A
{
    int a;
    mixin(bitfields!(
        uint, "x",    2,
        int,  "y",    3,
        uint, "z",    2,
        bool, "flag", 1));
}
A obj;
obj.x = 2;
obj.z = obj.x;

could become something like

struct A
{
    int a;
    mixin(bitfields!(
        uint, "x",    2, "@Foo @Bar(12)"
        int,  "y",    3,
        uint, "z",    2, "@Foo"
        bool, "flag", 1));
}
A obj;
obj.x = 2;
obj.z = obj.x;

but that would require making it so that bitfields looks at the types of
what it's passed to see if it has the extra string (similar to what
std.getopt.getopt does), and if it does, then it mixes in that string as
attributes on that field. It's a bit a of a pain to do, but it could be
done. What would make it worse though is if you want to try and make it so
that the attributes can be applied to just the setters or just the getters.
Then you'd probably end up with stuff like

struct A
{
    int a;
    mixin(bitfields!(
        uint, "x",    2, "@Foo @Bar(12)", "@Foo"
        int,  "y",    3,
        uint, "z",    2, "@Foo", "",
        bool, "flag", 1));
}
A obj;
obj.x = 2;
obj.z = obj.x;

which is getting a bit ugly IMHO. But it would be possible.

In any case, that's the best approach that I can think of at the moment.

- Jonathan M Davis



More information about the Digitalmars-d mailing list