Experimenting with variable reuse within declarations and its issues

In my case I often come across wanting this whenever one proc contains multiple proc calls of the same package which return the same error. Looked in my quick-scripts in Odin and here are few examples (disclaimer: I consider myself a begginner in Odin):

// here open_err, dir_err could be the same err
walk_proc :: proc(info: os.File_Info, in_err: os.Errno, user_data: rawptr) -> (err: os.Errno, skip_dir: bool) {
  handle, open_err := os.open(info.fullpath, os.O_RDONLY)
  if (open_err != 0) {
    fmt.eprintf("error opening directory: %s, error=%d", info.fullpath, open_err)
    return 0, true
  }
  files, dir_err := os.read_dir(handle, -1)
  if (dir_err != 0) {
    fmt.eprintf("error reading directory: %s, error=%d", info.fullpath, dir_err)
    return 0, true
  }
  // ...
}

Another one (with asserts, but could be proper error handling):

create_mock_users :: proc() -> []User {
  year, month, _ := time.date(time.now())
  day, err_month := dt.last_day_of_month(year, month)
  assert(err_month == .None)
  epoch_date, err_components := dt.components_to_date(2023, 11, 30)
  assert(err_components == .None)
  // ...
}

Another one I had somewhere is making multiple http calls each of which returns (response, err). Etc, etc.

3 Likes