Why D Needs Attributes (Was: Command-line arguments)
Lutger
lutger.blijdestijn at gmail.com
Fri Jul 4 06:10:45 PDT 2008
Nick Sabalausky wrote:
...
>
> The getopt version forces you to list each variable three times. Once for
> the variable itself, once more to tell getopt what it's called, and a
> third time to tell getopt where it is. That's highly redundant. They're
> only
> listed once for the C# version. (The getopt version could probably be
> changed to extract the name from the variable using templates and traits
> though, and it could probably eliminate the rest of that redundancy by
> sticking the vars into a class or stuct, but then it would lose the
> ability to let you specify options for each var. Attributes would bring
> that ability back without creating reduncancy).
I don't understand why attributes are needed to achieve this. You can indeed
use traits (with a mixin) to eliminate the redunancy of getopt, this takes
about 20 lines of code at most. With mixin+templates, it should not be too
difficult to also let the user specify options per variable. C# attributes
as I understand it make use of runtime reflection, D and especially D2 has
enough compile time reflection power to make this design work and do so in
a less verbose manner.
More information about the Digitalmars-d
mailing list