pure, safe and generalized attributes

Knud Soerensen 4tuu4k002 at sneakemail.com
Sat Apr 5 08:44:50 PDT 2008


Koroskin Denis wrote:
> Good, but I'd rather see 'thread-safe' built-in attribute.
> 
Yes, a threat-safe attribute would be very useful.

But if we can utilise the same code that help
implement build-in attributes like const, pure or thread-safe
to define user defined constrains
that would be a much better utilisation of the code and make D more
powerful.

> On Fri, 04 Apr 2008 11:19:49 +0400, Knud Soerensen
> <4tuu4k002 at sneakemail.com> wrote:
> 
>>
>> With all the talk about pure, safed and transitivity.
>> I think the time is right for a discussion about
>> user defined code attributes.
>>
>> Code attributes is just meta data for the code.
>> Normally, this meta data is stored in revision systems, databases, in
>> the code comments or just plain left out.
>> But there is some advantages by placing them in the code and letting the
>> compiler take care of them.
>>
>> Scott Meyers does a good job of introducing the subject in
>> http://video.google.com/videoplay?docid=-4728145737208991310
>>
>> ----
>>
>> The simplest use of code attribute I can imagine
>> is a attribute which indicate that this piece of code have been review.
>> It could be defined by.
>> attribute KilroyWasHere;
>>
>> and used like
>> int func(param p1) KilroyWasHere
>> {
>> ...
>> }
>>
>> You can do this with comments as well, but imagine you use.
>>
>> attribute FixMe warn;
>>
>> and the compile gave you a warning every time it sees the FixMe
>> attribute.
>>
>> attribute FixMe fail;
>>
>> Would give you a error and the build would fail.
>>
>> ----
>>
>> The next attribute indicate that the code is license under the GPL.
>>
>> attribute GPL transitive;
>>
>> Here the transitive keyword tells the compiler that the attribute is
>> transitive, and the compiler should check that every function called
>> also has the attribute GPL.
>> ----
>>
>> attribute LGPL deny GPL;
>>
>> This attribute is not transitive but the code should not call GPL code.
>> -----
>>
>> attribute safe transitive allow pure;
>>
>> Define an attribute for thread-safety which is transitive but also
>> allows pure functions. pure would be a build in attribute because it
>> need to be able to validate the code.
>> -----
>>
>> Now imagine a big team need to refactor a big ugly code base.
>>
>> Then the following attributes will allow the team to mark
>> the code after how it smells (good, okay or bad);
>>
>> attribute good transitive warn;
>> attribute okay transitive warn allow good;
>> attribute bad transitive warn allow okay,good;
>>
>> The attributes only issue warnings, so they wouldn't stop the code form
>> compiling, and only when more smelly code is called.
>>
>>
>> I hope that I have illustraded that user definded code attributes
>> could be very useful.
>>
>> Knud
> 



More information about the Digitalmars-d mailing list