Where to place function attributes?

Jonathan M Davis jmdavisProg at gmx.com
Sun Aug 19 01:15:42 PDT 2012


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


More information about the Digitalmars-d-learn mailing list