Template help - MultiAccess

Philippe Sigaud philippe.sigaud at gmail.com
Tue Jan 8 15:17:38 PST 2013


On Tue, Jan 8, 2013 at 10:37 PM, Era Scarecrow <rtcvb32 at yahoo.com> wrote:
>
>  Well after getting the alias problem out of the way, I have a working template. If anyone cares to look it over and critique it. I'll consider adding it to phobos (assuming I didn't miss something similiar), although I am not sure where it would be added as it doesn't quite qualify for std.traits, std.bitmanip, maybe std.functional?
>
>  https://github.com/rtcvb32/Side-Projects/blob/master/multiaccess.d


Some comments:

mixin(multiAccess!(
                int,        //return type
                "test",     //function call/name
                "nothrow",  //attributes
                choice,     //variable/call that determines which to call
                true,       //make read function
                true,       //make write function
                a, false,   //choose a if 'choice' is false
                b, true));  //choose b if 'choice' is true

The 'int,' part is not necessary: names a and b have a type, you can
determine int from CommonType!(typeof(a),typeof(b)). One less field
for your user.

Concerning the 'true'/'false' parts for the read/write function: I'm
not sure they are necessary. If you want your host to be a multi
access struct, you sure want people to access the fields...
Should you still choose to let these parameters, please use two enums:
enum ReadAccess { no, yes } and WriteAccess {no, yes }. This makes for
a more explicit code when calling the template:

...
choice,
ReadAccess.yes,
WriteAccess.no,
...

For the (a, true) pairs, I'd invert the arguments: first the value,
then the effect (in this case, which field to access).

As for the global usefulness of your mixin, I don't know. I never felt
a need for such a construction, so I can't comment much more on it.


More information about the Digitalmars-d-learn mailing list