www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Where to place function attributes?

reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
This probably isn't specifically a D only question as I've seen 
this in C++ too, but does it make any kind of difference where an 
attribute is placed when writing a function?

example

class C
{
     immutable void foo()
     {
         //code
     }
}

vs
class D
{
     void foo() immutable
     {
         //code
     }
}
Aug 19 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, August 19, 2012 09:57:14 Jeremy DeHaan wrote:
 This probably isn't specifically a D only question as I've seen
 this in C++ too, but does it make any kind of difference where an
 attribute is placed when writing a function?
No. Any function attribute can go on either side, and which side they go on is generally personal preference. const is a funny one though (as is immutable), since putting it on the left is the same as putting it on the right (as with all of the other attributes), but this can throw you off, because you might think that it's part of the return type when it isn't. e.g. const C func() {...} is the same as C func() const {...} If you want the return type to be const, you need to use parens: const(C) func() {...} So, it's generally considered good practice to put const and immutable on the right-hand side. - Jonathan M Davis
Aug 19 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 19 Aug 2012 10:15:42 +0200, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

  it's generally considered good practice to put const and immutable on  
 the right-hand side.
I would also say that putting function attributes on a separate line above the function is fairly common: const property pure int foo() { return 3; } -- Simen
Aug 19 2012
prev sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Sunday, 19 August 2012 at 07:57:15 UTC, Jeremy DeHaan wrote:
 This probably isn't specifically a D only question as I've seen 
 this in C++ too, but does it make any kind of difference where 
 an attribute is placed when writing a function?
I've gotten in a habit of putting it to the right. struct X { string something() safe const pure nothrow { ... } }
Aug 19 2012