June 12, 2013 Re: User Defined Attributes (UDA) in Phobos/druntime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 12 June 2013 at 06:46:06 UTC, Jacob Carlborg wrote: > Yes, I would like to have the language enforce this. My utility function fetches by default types that are marked with @attribute. You can add flag when calling the function to fetch all UDA's. > >> BTW I've just found one use case for anonymous UDA: >> @(Enum.Entry) is verbose, the question is: "Is it useful enough to keep >> it, or maybe having single convention is better?" > > Is it possible to attach a UDA to an enum member? Or is it possible to figure out that it's an enum member and check if the enum itself has @attribute attached to it? Accrding to this http://dpaste.dzfl.pl/728872fa it's not possible to attach an UDA to enum member. I think that what you propose, together with parametrisation made by IdanArye to your @attribute definition is a nice compromise for now. It allows reuse of utils and interoperation, sets up a convention to follow and does not enforce language changes. But it will only work if it's going to be in phobos for example as std.attribute. In my opinion such compromise would be easier to get into the language than fully enforcing the convention and I think it's very important to have some form of UDAs for serialization just for users' convenience. |
June 12, 2013 Re: User Defined Attributes (UDA) in Phobos/druntime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2013-06-12 14:29, Andrej Mitrovic wrote: > But without this you lose the ability to customize. I don't like just > "tagging" something, I like D's ability where you can actually > customize the UDA, for example: > > ----- > import std.typetuple; > > struct Server > { > string url; > } > > struct Config > { > @(Server("http://foo.bar")) string name; > @(Server("http://doo.bar")) string[] items; > } > > template GetAttributes(T...) if (T.length == 1) > { > alias GetAttributes = TypeTuple!(__traits(getAttributes, T[0])); > } > > void main() > { > Config config; > > alias attribs = GetAttributes!(config.name); > > static if (attribs.length && is(typeof(attribs[0]) == test.Server)) > { > pragma(msg, attribs[0]); > } > } > ----- > > Thanks to CTFE we can tag a field with a struct that may have any > state. All you have to do in the library code is to first check if any > of the attributes is an "attribute type (a struct of some sort)" that > you recognize, and then read its state. As far as I know other language allows UDA's to be of any type/value. Java uses the @annotation annotation on an interface and in C# classes with System.Attribute as the base class are used. -- /Jacob Carlborg |
June 13, 2013 Re: User Defined Attributes (UDA) in Phobos/druntime | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Wednesday, 12 June 2013 at 07:46:31 UTC, deadalnix wrote:
> On Tuesday, 11 June 2013 at 07:36:44 UTC, Walter Bright wrote:
>> On 6/11/2013 12:00 AM, Jacob Carlborg wrote:
>>> Thoughts?
>>
>> I'd like to see more use of UDAs in non-Phobos code so we can figure out best practices from experience before putting it into Phobos, where we'll be stuck with any bad decisions.
>
> That sound good, but the complete opposite have been done every single time. That raise the question of consistency.
And that's why Walter wants to avoid repeating the same mistakes again.
|
Copyright © 1999-2021 by the D Language Foundation