Enum

nil

  • From my understanding, if the value of a enum is nil , that means that the result is equal to the first element in the enum.

  • For example:

Division_Error :: enum {
    Division_By_Zero,
}

divide :: proc(a, b: f32) -> (result: f32, err: Division_Error) {
    if b == 0 {
        return 0, .Division_By_Zero
    }
    return a / b, nil
}

_, err := divide(2, 0)
if err != nil {
    fmt.printfln("There was an error! '%v'", err)
}
  • The code above is wrong, as it will never print that an error has occurred. .Division_By_Zero   IS   nil , so the condition if err != nil {}  will never happen.

  • The enum should be defined as:

Division_Error :: enum {
    None, // or "Ok", "No_Error", or whatever.
    Division_By_Zero,
}
  • In conclusion, as the first value will always be the same as nil , the first value should signify "no error".