How can this "function ends without a return statement" be right?

Showing 1-9 of 9 messages
How can this "function ends without a return statement" be right? josvazg 11/14/11 10:39 PM
I have a couple of functions that look like this:

func somefunc(saddr string) (FinalResultType, os.Error) {
        if result, err := doSomeCalculation(); e != nil {
                return nil, e
        } else {
                return wrapIt(result), nil
        }
//        return nil,nil
}

Even though the third and last return (I believe) IS DEAD CODE, If I
don't include it (I left it commented as in the sample) I get the
following error:

"function ends without a return statement"

Which I think it is a plain lie! (maybe a bug) cause it either returns
on the IF clause or the ELSE clause, there IS NO WAY it can go through
a path with NO return statement... or is there?

What I am missing here?
Re: [go-nuts] How can this "function ends without a return statement" be right? Miek Gieben 11/14/11 10:46 PM
[ Quoting <jos...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]

> I have a couple of functions that look like this:
>
> func somefunc(saddr string) (FinalResultType, os.Error) {
>         if result, err := doSomeCalculation(); e != nil {
>                 return nil, e
>         } else {
>                 return wrapIt(result), nil
>         }
> //        return nil,nil
> }
>
> Even though the third and last return (I believe) IS DEAD CODE, If I
> don't include it (I left it commented as in the sample) I get the
> following error:
>
> "function ends without a return statement"

Its a compiler bug, make the function look like:

func somefunc(saddr string) (FinalResultType, os.Error) {
         if result, err := doSomeCalculation(); e != nil {
                 return nil, e
         }
         return wrapIt(result), nil
}


 grtz,

--
    Miek

Re: [go-nuts] How can this "function ends without a return statement" be right? r 11/14/11 10:49 PM

The compilers require either a return or a panic to be lexically last in a function with a result. This rule is easier than requiring full flow control analysis to determine whether a function reaches the end without returning (which is very hard in general), and simpler than rules to enumerate easy cases such as this one. Also, being purely lexical, the error cannot arise spontaneously due to changes in values such as constants used in control structures inside the function.

-rob

Re: [go-nuts] How can this "function ends without a return statement" be right? r 11/14/11 10:50 PM

On Nov 14, 2011, at 10:46 PM, Miek Gieben wrote:

> [ Quoting <jos...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]
>> I have a couple of functions that look like this:
>>
>> func somefunc(saddr string) (FinalResultType, os.Error) {
>>         if result, err := doSomeCalculation(); e != nil {
>>                 return nil, e
>>         } else {
>>                 return wrapIt(result), nil
>>         }
>> //        return nil,nil
>> }
>>
>> Even though the third and last return (I believe) IS DEAD CODE, If I
>> don't include it (I left it commented as in the sample) I get the
>> following error:
>>
>> "function ends without a return statement"
>
> Its a compiler bug, make the function look like:

It's not a bug, it's a deliberate design decision.

-rob


Re: How can this "function ends without a return statement" be right? josvazg 11/14/11 11:26 PM
Ok, but then I need to define the result outside the if clause like
this:

func somefunc(saddr string) (FinalResultType, os.Error) {
        result, err := doSomeCalculation();
        if e != nil {
                return nil, e
        }
        return wrapIt(result), nil
}

Otherwise the result scope is lost befor ethe last return statement

On Nov 15, 7:46 am, Miek Gieben <m...@miek.nl> wrote:
> [ Quoting <josv...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]
>  signature.asc
> < 1KViewDownload
Re: How can this "function ends without a return statement" be right? josvazg 11/14/11 11:33 PM
Ok, Thanks!

I will take this into account from now on.
I guess it's on the docs (probably the Specs) but I failed to remember
it...
Re: [go-nuts] How can this "function ends without a return statement" be right? Dave Cheney 11/14/11 11:35 PM
Hi

This is a long standing request,

http://code.google.com/p/go/issues/detail?id=65

It's generally possible to work around the issue by removing the else clause and let the logic fall through to a return at the bottom of the function.

Sent from my iPad

Re: [go-nuts] How can this "function ends without a return statement" be right? Miek Gieben 11/15/11 12:25 AM
[ Quoting <r...@google.com> at 22:50 on Nov 14 in "Re: [go-nuts] How ca..." ]

> > Its a compiler bug, make the function look like:
>
> It's not a bug, it's a deliberate design decision.

Ah, I still thought it was considered a bug. So
issue 65 can be closed? (just asking)

grtz Miek

Re: [go-nuts] How can this "function ends without a return statement" be right? Josef 11/15/11 3:59 PM
Hi,

If you define the variables in the in the first line, you'll keep the
nice formatting along with the scope.

func somefunc(saddr string) (result FinalResultType, err os.Error) {
    if result, err = doSomeCalculation(); err != nil {
return nil, err       }         return wrapIt(result), nil}

Regards
Josef