July 01, 2004
In article <cc0d87$13g$1@digitaldaemon.com>, dennis luehring says...
>>>@walter: what about defineable attributes for D?
>> I wouldn't mind a feature like that one bit, even if it does sound a bit like C#.
>
>which C# feature? name?

C#, and .NET in general, supports arbitrary attributes for almost anything in the language.  The idea is that you can assign attributes to classes, instances and such to give the compiler hints on what to do with a given item.   *Custom* attributes are only really useful via reflection, and allows you to do some pretty powerful things.  The custom attributes themselves are defined in more C# code and can be used within the same project sourcecode as where it is used.

This gives you a very powerful way to make class metadata far more meaningful than just raw class info.

For example, I once created a class that would initalize static members on classes, in an arbitrary assembly, from data in an XML file.  This utility would look for "[StaticInit]" attributes tagged to static struct and class members and set their values based on their name and hosting class.  This was accomplished through 100% reflection, including dynamically loading the assemblies into the runtime as referenced in the config file.

Similar techniques can be applied to *intellegently* implement transparent serialization, automatic proxy creation for remoting, etc etc.  There may be reasons why you wouldn't want to persist every member of a class or to do special things with making certain methods available over the wire.  A facility like this makes it all possible.

I for one would love to see a similar, or more advanced, set of features in the
next full release of D (version 2?). :)

Here's a pretty good article on the topic:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkattributestutorial.asp

- Pragma


July 03, 2004
dennis luehring wrote:
>>> Have you tried it? It should work fine.
>>> http://www.digitalmars.com/d/attribute.html
>>> D's attributes are brilliant.
>>
>> looks like what i want
> 
> 
> in some of my projects codegernators were used to "publish" methods through an command-pattern based plugin system...
> 
> can i use this feature for code generators:
> (in need a marker for my parser to find "to published functions")
> 
> for example:
> 
> (pseudo code)
> class Test
> {
>   private:
>     funct1
>     funct2
>   public:
>     funct3
>   for_the_generator:
>     funct4
>     funct5
> }
> 
> ciao dennis

Have you considered using version (http://www.digitalmars.com/d/version.html).

I'm not sure I understand the purpose of your proposed User Defineable Attributes, but I think you can effectively emulate them by making using of D's existing versioning features. It's a little more wordy than your proposal, but I think it'll work. Here's an example:

 version(myCodeGeneration) version = published;

class Plugin {
   public:
      void Function1(...)
      int Function2(...)
   version(published) {
      int Function3(...)
      int m_nMaxCount
   }
   private:

}


The code in the version block is only compiled if the myCodeGeneration or published version is set, such as:

dmd test.d version=myCodeGeneration


-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
1 2
Next ›   Last »