auto in library functions

Benjamin Thaut code at benjamin-thaut.de
Sun Dec 23 14:21:19 PST 2012


On Sunday, 23 December 2012 at 22:16:54 UTC, Jonathan M Davis 
wrote:
>
> Yeah. I don't know what they were complaining about in the pull 
> request, but I
> think that Andrej has it right. Basically, when declaring the 
> return type, if
> you need type inference, use auto, but if you don't, then put 
> the explicit
> type so that it's clearer in the documentation. druntime is 
> unlikely to need
> type inference, as its functions generally return simple, 
> explicit types
> rather than depending on the types passed to them like happens 
> often in
> Phobos. And they don't usually return types which are heavily 
> templatized,
> which is where auto return types really shine.
>
> Using auto for local variables, however, is completely 
> differente. There, it's
> frequently best to use it unless you need to declare a variable 
> as a specific
> type (e.g. size_t i = 0; instead of auto i = 0; when you need 
> size_t). It
> makes refactoring the code easier and reduces code breakage 
> when types change.
> But with return types, it affects the documentation and 
> people's ability to
> figure out what a function does, so as useful as auto return 
> types can be, they
> come with a definite cost and should be avoided if they're not 
> needed.
>
> - Jonathan M Davis

It's always about local variables.

Kind Regards
Benjamin Thaut



More information about the Digitalmars-d mailing list