digitalmars.D - pure, safe and generalized attributes
- Knud Soerensen (51/51) Apr 04 2008 With all the talk about pure, safed and transitivity.
- Janice Caron (13/13) Apr 04 2008 On 04/04/2008, Knud Soerensen <4tuu4k002@sneakemail.com> wrote:
- Leandro Lucarella (14/31) Apr 04 2008 Or something simpler, like Python's decorators:
- bearophile (10/16) Apr 04 2008 Do you mean to copy just the syntax? Because Python decorator semantics ...
- Knud Soerensen (2/28) Apr 05 2008 I like the idea with @ as short for (at)tribute.
- Knud Soerensen (4/21) Apr 05 2008 You could also ague that it is not necessary because
- Janice Caron (3/6) Apr 05 2008 KilroyWasHere:
- Knud Soerensen (4/13) Apr 05 2008 "KilroyWasHere" is the label for the attribute
- Janice Caron (11/24) Apr 05 2008 I was trying to make the point that D has a context-free grammar, and
- Knud Soerensen (21/52) Apr 05 2008 I see that the D manual already have something called attributes
- Janice Caron (9/11) Apr 05 2008 Yes it was. I assumed that "attribute" meant what it means in the D
- Georg Wrede (2/18) Apr 09 2008 This would be enormously confusing to people reading others' code.
- Koroskin Denis (7/59) Apr 04 2008 Good, but I'd rather see 'thread-safe' built-in attribute.
- Knud Soerensen (7/91) Apr 05 2008 Yes, a threat-safe attribute would be very useful.
- David Wilson (14/29) Apr 04 2008 For all that I'd love to see something like .NET attributes in D, if
- Knud Soerensen (8/43) Apr 05 2008 Yes, if you only read the part of my post that you have cited
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
Apr 04 2008
On 04/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote: <snip> Nice. But to avoid accruing an infinite number of keywords, the syntax needs to be slightly changed. Instead of int func(param p1) KilroyWasHere { ... } use int func(param p1) attribute(KilroyWasHere) { ... }
Apr 04 2008
Janice Caron, el 4 de abril a las 08:42 me escribiste:On 04/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote: <snip> Nice. But to avoid accruing an infinite number of keywords, the syntax needs to be slightly changed. Instead of int func(param p1) KilroyWasHere { ... } use int func(param p1) attribute(KilroyWasHere) { ... }Or something simpler, like Python's decorators: KilroyWasHere int func(param p1) { ... } -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- All men are born equal But quite a few get over it
Apr 04 2008
Leandro Lucarella:Or something simpler, like Python's decorators: KilroyWasHere int func(param p1) { ... }Do you mean to copy just the syntax? Because Python decorator semantics is quite different, they are a way to feed a function/method to one of more metafunctions :-) So this: foo int func(param p1) {} means something like: func = foo(func) And there are more complex usages, stacking more than one decorators, giving them arguments, etc. Bye, bearophile
Apr 04 2008
Leandro Lucarella wrote:Janice Caron, el 4 de abril a las 08:42 me escribiste:I like the idea with as short for (at)tribute.On 04/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote: <snip> Nice. But to avoid accruing an infinite number of keywords, the syntax needs to be slightly changed. Instead of int func(param p1) KilroyWasHere { ... } use int func(param p1) attribute(KilroyWasHere) { ... }Or something simpler, like Python's decorators: KilroyWasHere int func(param p1) { ... }
Apr 05 2008
Janice Caron wrote:On 04/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote: <snip> Nice. But to avoid accruing an infinite number of keywords, the syntax needs to be slightly changed. Instead of int func(param p1) KilroyWasHere { ... } use int func(param p1) attribute(KilroyWasHere) { ... }You could also ague that it is not necessary because attributes can only appear in certain places only in function, class, struct and module declarations.
Apr 05 2008
On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:You could also ague that it is not necessary because attributes can only appear in certain places only in function, class, struct and module declarations.KilroyWasHere: Is that an attribute or a label?
Apr 05 2008
Janice Caron wrote:On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:"KilroyWasHere" is the label for the attribute that this place was inspected by Kilroy Why do you ask ??You could also ague that it is not necessary because attributes can only appear in certain places only in function, class, struct and module declarations.KilroyWasHere: Is that an attribute or a label?
Apr 05 2008
On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:Janice Caron wrote:I was trying to make the point that D has a context-free grammar, and that an identifier followed by a colon, where you might expect to find a statement or a declaration, might be ambiguous. In hindsight, I might have jumped too fast. It might be possible to tell the difference because: if you're expecting a statement, it's a label; if you're expecting a declaration, it's an attribute. However, the point still stands. Allowing arbitrary identifiers as attributes is /bound/ to confuse the parser somewhere along the line, unless we explicitly indicate to the compiler that this identifier is an attribute.On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:>> You could also ague that it is not necessary because >> attributes can only appear in certain places >> >> only in function, class, struct and module declarations. >KilroyWasHere:> > Is that an attribute or a label? "KilroyWasHere" is the label for the attribute that this place was inspected by Kilroy Why do you ask ??
Apr 05 2008
Janice Caron wrote:On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:I see that the D manual already have something called attributes it might have been confusing you. Maybe if i use the word constrain, for what we talking about is a way to define code constrains. We have this now i D ModuleDeclaration: module ModuleName ; ClassDeclaration: class Identifier BaseClassList(opt) ClassBody FunctionDeclaration: (I didn't find this in the manual) type Identifier(Parameters) FunctionBody With constrains we will have this ModuleDeclaration: module ModuleName ConstrainList(opt); ClassDeclaration: class Identifier BaseClassList(opt) ConstrainList(opt) ClassBody FunctionDeclaration: type Identifier(Parameters) ConstrainList(opt) FunctionBody Hope this helps KnudJanice Caron wrote:I was trying to make the point that D has a context-free grammar, and that an identifier followed by a colon, where you might expect to find a statement or a declaration, might be ambiguous. In hindsight, I might have jumped too fast. It might be possible to tell the difference because: if you're expecting a statement, it's a label; if you're expecting a declaration, it's an attribute. However, the point still stands. Allowing arbitrary identifiers as attributes is /bound/ to confuse the parser somewhere along the line, unless we explicitly indicate to the compiler that this identifier is an attribute.On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:>> You could also ague that it is not necessary because >> attributes can only appear in certain places >> >> only in function, class, struct and module declarations. >KilroyWasHere:> > Is that an attribute or a label? "KilroyWasHere" is the label for the attribute that this place was inspected by Kilroy Why do you ask ??
Apr 05 2008
On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:I see that the D manual already have something called attributes it might have been confusing you.Yes it was. I assumed that "attribute" meant what it means in the D grammar docs. This wasn't the first time that confusion was caused by two people assuming the same word meant two different things. I'm sure it won't be the last! :-) That said, you'd probably want your constraints to be attributes anyway. That is, you'd want to put them anywhere you can currently put "static", "private", etc.
Apr 05 2008
Janice Caron wrote:On 05/04/2008, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:This would be enormously confusing to people reading others' code.I see that the D manual already have something called attributes it might have been confusing you.Yes it was. I assumed that "attribute" meant what it means in the D grammar docs. This wasn't the first time that confusion was caused by two people assuming the same word meant two different things. I'm sure it won't be the last! :-) That said, you'd probably want your constraints to be attributes anyway. That is, you'd want to put them anywhere you can currently put "static", "private", etc.
Apr 09 2008
Good, but I'd rather see 'thread-safe' built-in attribute. On Fri, 04 Apr 2008 11:19:49 +0400, Knud Soerensen = <4tuu4k002 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 t=hecompiler take care of them. Scott Meyers does a good job of introducing the subject in http://video.google.com/videoplay?docid=3D-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 for=mcompiling, and only when more smelly code is called. I hope that I have illustraded that user definded code attributes could be very useful. Knud
Apr 04 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 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
Apr 05 2008
On Fri, Apr 4, 2008 at 7:19 AM, Knud Soerensen <4tuu4k002 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;For all that I'd love to see something like .NET attributes in D, if it means people starting to mark functions and fields with [BugFix(1234)] or [ReviewedBy("me")] then count me out! Source files, i.e. the data structures you feed to a compiler, really isn't the place for adding support for ticket tracking or a review process. :) For the first case, get a revision control system and use its annotation function (this has the benefit of systematically recording all changes to every line of code in a file, not just ones some programmer arbitrarily decided to tag with an attribute). For the latter case, see something like review-board.org or search Google for "QA branch". These really aren't good uses of custom attributes at all. David.
Apr 04 2008
David Wilson wrote:On Fri, Apr 4, 2008 at 7:19 AM, Knud Soerensen <4tuu4k002 sneakemail.com> wrote:Yes, if you only read the part of my post that you have cited then there is no point of implementing it. That why I write the you can do that with comment as well. The real power comes when you utilise the compiler to ensure the transitivity constraint of the attribute, something most revision control system can't do and something I think will be very helpful when dealing with big code bases and/or big teams.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;For all that I'd love to see something like .NET attributes in D, if it means people starting to mark functions and fields with [BugFix(1234)] or [ReviewedBy("me")] then count me out! Source files, i.e. the data structures you feed to a compiler, really isn't the place for adding support for ticket tracking or a review process. :) For the first case, get a revision control system and use its annotation function (this has the benefit of systematically recording all changes to every line of code in a file, not just ones some programmer arbitrarily decided to tag with an attribute). For the latter case, see something like review-board.org or search Google for "QA branch". These really aren't good uses of custom attributes at all.
Apr 05 2008