auto in library functions

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Dec 23 14:05:28 PST 2012


On 12/23/12, Benjamin Thaut <code at benjamin-thaut.de> wrote:
> I just created my first pull request for druntime and there where
> multiple commonets to _not_ use auto. I don't quite understand
> why it shouldn't be used in library functions. If it should not
> be used why is it in the language in the first place?
>
> Kind Regards
> Benjamin Thaut

auto is mainly useful for complex return types or for nested types
which you can't otherwise declare (called voldemort types), e.g.:

auto x = joiner([""], "xyz");
writeln(typeid(x)); //  => std.algorithm.joiner!(string[], string).joiner.Result

When the type is simple you should use the name of the type instead of
auto because it serves as useful documentation, especially in library
functions where you might later have to fix a bug. It's really useful
to know exactly what type each variable is without having to waste
time e.g. looking at function declarations to figure out the return
type.


More information about the Digitalmars-d mailing list