V: function returns null

Created on 4 May 2020  路  6Comments  路  Source: vlang/v

V version:
V 0.1.26 50a8373.9aa1a65
OS:
linux amd64

What did you do?

fn a() string {
    """same with this code
    match 1 {
        1 {

        }
        else {
            return ""
        }
    }
    """
    if false {
        return "a"
    }
}


fn main() {
    a_null := a()
    println(a_null)
}

What did you expect to see?
compiler error: "error: missing return at end of function a"

What did you see instead?
(null)

Bug Confirmed

All 6 comments

yeah it results in an error in simple cases like

fn a() string {}

Maybe for now, when no full analysis is implemented, it could be implemented as: require a return statement of valid return type at same scope as the function body. Additional conditions like if and match are allowed.

The following valid code woulde be illegal after this change (until no full analysis is implemented):

fn a() string {
  if 1 == 1 {
    return "1"
  } else {
   return "2"
  }
}

The full analysis was implemented in V1, so it should be trivial to bring it back.

Doing this will break lots of existing code.

Ok, then for now you could just set a default value of T as return value for this case, I think that wouldnt break code. This would also prevent null cases, if the full analysis had unchecked cases / bugs.

Now is a good time to break code this way. Lots of other things have been broken along the way from V1 to V2, so better to break this now than carry that technical debt into the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jtkirkpatrick picture jtkirkpatrick  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

clpo13 picture clpo13  路  3Comments

taojy123 picture taojy123  路  3Comments