Using consistently auto as function return type

Era Scarecrow rtcvb32 at yahoo.com
Sat Jun 16 02:43:23 PDT 2012


On Saturday, 16 June 2012 at 09:31:35 UTC, Tommi wrote:
> Do you consider it to be good or bad style of programming to 
> use consistently auto as function return type?
>
> One of the pros is that it saves some redundant typing when the 
> function returns some complex templated type:
>
> auto getValue()
> {
>     return MyType!(int, "asdf", 64).init;
> }
>
> But one of the cons is that you don't see what the function 
> returns just by looking at the signature.
>
> Are there some more severe issues that I'm missing?

  I've wondered a bit of this myself. I tend to have all my 
function return types explicit and tend to use auto more for 
values returned when I am going to be using them right away, or 
they vary slightly on type (but not function).

  Being as D is not C++ (with it's template system), it can still 
parse and make sense of a majority of the code but lacking a 
specific return type... I want to say it's a potential pitfall 
and trap for later. If it only returns a basic type then the 
problem isn't as severe since implicit casting can take place to 
fix minor variations of type (real/double, int/long, etc).

  Perhaps a suggestion on use, where auto should be used: As a 
lambda return type, or functions that return simple base types?  
Not to say it would prevent you from from using auto, but then 
the potential damage is lessened. Perhaps a bigger problem is 
that auto would hide type changes and potential problems because 
it was auto, but regarding templates with that I don't know. 
Mmmm... I need more experience.


More information about the Digitalmars-d-learn mailing list