Thread overview
Re: return in void functions
Mar 05, 2009
bearophile
Mar 06, 2009
Georg Wrede
Mar 06, 2009
Nick Sabalausky
Mar 06, 2009
bearophile
March 05, 2009
Nick Sabalausky:
> There has to be a better way to handle that.

Possible idea: allowing functions too (and not just function templates as in D2) to have an "auto" return type?


>(In fact, I've been bitten by that before.)<

Me too, that's why I have started this thread.

Bye,
bearophile
March 06, 2009
bearophile wrote:
> Nick Sabalausky:
>> There has to be a better way to handle that.
> 
> Possible idea: allowing functions too (and not just
> function templates as in D2) to have an "auto" return type?

void main()
{
    auto a = f();
    // Here, store a in a struct, i.e. you need to know the type
    // so that you can have the right kind of struct for it.
}

auto f()
{
    auto c = g();
    return c;
}


auto g()
{
    auto c = h();
    return c;
}

Then in a library (probably not even documented with the actual return type -- an understandable omission by this time...)

auto h()
{
    auto c = i();
    return c;
}

auto i()
{
    auto c = j(); // where j() is a non-public function.
    return c;
}


...etc. So, isn't it easier for the programmer to know what the data type is, without going for goose chases every time?
March 06, 2009
"Georg Wrede" <georg.wrede@iki.fi> wrote in message news:goqlhm$bg5$1@digitalmars.com...
> bearophile wrote:
>> Nick Sabalausky:
>>> There has to be a better way to handle that.
>>
>> Possible idea: allowing functions too (and not just function templates as in D2) to have an "auto" return type?
>
> void main()
> {
>     auto a = f();
>     // Here, store a in a struct, i.e. you need to know the type
>     // so that you can have the right kind of struct for it.
> }
>
> auto f()
> {
>     auto c = g();
>     return c;
> }
>
>
> auto g()
> {
>     auto c = h();
>     return c;
> }
>
> Then in a library (probably not even documented with the actual return type -- an understandable omission by this time...)
>
> auto h()
> {
>     auto c = i();
>     return c;
> }
>
> auto i()
> {
>     auto c = j(); // where j() is a non-public function.
>     return c;
> }
>
>
> ...etc. So, isn't it easier for the programmer to know what the data type is, without going for goose chases every time?

You could probably just do:

auto a = f();
Stdout.formatln("{}", typeof(a).stringof); // Debug

But I still wouldn't want to have to do that.

Of course, that could arguably be solved by having automatic documentation automatically resolve the "auto". Although that would require 3rd party doc generators to do much more involved parsing.


March 06, 2009
Nick Sabalausky:
> Of course, that could arguably be solved by having automatic documentation automatically resolve the "auto".

This seems a ncie idea for ddoc.

Bye,
bearophile