www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - On Attributes

reply A Guy With a Question <aguywithaquestion gmail.com> writes:
Hi again!

I've been trying to do my best to write idiomatically. One thing 
that is bugging me is having to mark up all of my declarations 
with attributes. Which means I'm having to remember them all. 
It's a bit much to keep in my head with every function. Is there 
a good way to reverse this (imply the attributes by default) and 
then turn them off explicitly? Like declaring them at the top of 
the file so they apply to everything below?

Thanks!
Nov 27 2017
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/27/17 2:10 PM, A Guy With a Question wrote:
 Hi again!
 
 I've been trying to do my best to write idiomatically. One thing that is 
 bugging me is having to mark up all of my declarations with attributes. 
 Which means I'm having to remember them all. It's a bit much to keep in 
 my head with every function. Is there a good way to reverse this (imply 
 the attributes by default) and then turn them off explicitly? Like 
 declaring them at the top of the file so they apply to everything below?
You can have some degree of success with global application and inference. Note that ALL attributes can be applied in one of 3 ways: 1. At the declaration: safe int foo(); safe int bar(); 2. In a scope: safe { int foo(); int bar(); } 3. as a label: safe: int foo(); int bar(); There are some limitations, such that it will NOT apply to functions inside classes or structs. Also, attributes without an opposite attribute (e.g. pure) cannot be undone once you apply at the top with a label. Note that templates automatically infer attributes, you do not have to write them: T foo(T)() {... } // implies safe if possible Functions which return auto also imply attributes: auto foo() {... } // implies safe if possible The reason here is because the implementation of the function must be available to properly compile, therefore the compiler can infer the proper attributes. Hope this helps. -Steve
Nov 27 2017
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Nov 27, 2017 at 07:10:04PM +0000, A Guy With a Question via
Digitalmars-d-learn wrote:
 Hi again!
 
 I've been trying to do my best to write idiomatically. One thing that
 is bugging me is having to mark up all of my declarations with
 attributes.  Which means I'm having to remember them all. It's a bit
 much to keep in my head with every function. Is there a good way to
 reverse this (imply the attributes by default) and then turn them off
 explicitly? Like declaring them at the top of the file so they apply
 to everything below?
[...] You can declare attributes at the top of the file by writing "attr:" where "attr" is pure, nothrow, nogc, etc.. However, there is currently no way to negate these attributes afterwards. An alternative is to let the compiler do the hard work for you by templatizing your code. Template functions and members of templated aggregates (structs and classes) trigger attribute inference. You can turn non-template functions into template functions by adding an empty list of compile-time parameters: int myfunc()(int x, int y) { ... } This has the added advantage that if myfunc is never actually referenced elsewhere in the code, it won't even be compiled into the executable. Of course, this is not a perfect solution, since you can't use it if for whatever reason your function must not be a template. One area is class methods that must be overridden by derived classes, which can't be template methods. There's no way around specifying explicit attributes in that case. T -- The fact that anyone still uses AOL shows that even the presence of options doesn't stop some people from picking the pessimal one. - Mike Ellis
Nov 27 2017
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 27 November 2017 at 19:10:04 UTC, A Guy With a
 One thing that is bugging me is having to mark up all of my 
 declarations with attributes.
Meh, you could also just ignore the attribute crap. Only reason I ever mess with them is if someone who is using them tries to use my code... otherwise you can pretend the spam doesn't exist and be more productive.
Nov 27 2017
parent reply A Guy With a Question <aguywithaquestion gmail.com> writes:
On Monday, 27 November 2017 at 19:41:03 UTC, Adam D. Ruppe wrote:
 On Monday, 27 November 2017 at 19:10:04 UTC, A Guy With a
 One thing that is bugging me is having to mark up all of my 
 declarations with attributes.
Meh, you could also just ignore the attribute crap. Only reason I ever mess with them is if someone who is using them tries to use my code... otherwise you can pretend the spam doesn't exist and be more productive.
Yeah, I'm leaning towards that direction. It seems they could have been useful if they were the default. But opting into them doesn't seem as useful, unfortunately. I'll probably continue fiddling with them, but I might just abandon using them.
Nov 27 2017
parent user1234 <user1234 12.nl> writes:
On Monday, 27 November 2017 at 20:07:08 UTC, A Guy With a 
Question wrote:
 On Monday, 27 November 2017 at 19:41:03 UTC, Adam D. Ruppe 
 wrote:
 On Monday, 27 November 2017 at 19:10:04 UTC, A Guy With a
 One thing that is bugging me is having to mark up all of my 
 declarations with attributes.
Meh, you could also just ignore the attribute crap. Only reason I ever mess with them is if someone who is using them tries to use my code... otherwise you can pretend the spam doesn't exist and be more productive.
Yeah, I'm leaning towards that direction. It seems they could have been useful if they were the default. But opting into them doesn't seem as useful, unfortunately. I'll probably continue fiddling with them, but I might just abandon using them.
I also rarely put the attributes (although sincerely i think that my funcs are pure 99% of them time) but take care because recently it was announced that better code will be generated if there are scope(exit) / try-finally inside nothrow funcs: https://forum.dlang.org/thread/ovbduq$m3a$1 digitalmars.com
Nov 27 2017