Beginner's Guide to Zig Part 4 - Errors

Поділитися
Вставка
  • Опубліковано 21 лис 2024

КОМЕНТАРІ • 3

  • @songxu7837
    @songxu7837 4 місяці тому

    I like the example

  • @skiiwars8991
    @skiiwars8991 Місяць тому

    Does including an exclamation point for functions returning void mean that the function could return either void or an error? And if so, would running
    if(result) |val|
    else |err|
    Always go to the else statement (assuming zig allows comparing void or casting void to bool)
    Is this case different or does it work the same as any other type?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  Місяць тому +1

      I don't think you can capture the variable like this:
      if (result) |val|
      You can't use a void type, so capturing won't work. You can do this though:
      if (result) {
      std.debug.print("No errors
      ", .{});
      }
      else |err| {
      std.debug.print("Error {}
      ", .{err});
      }
      If you just return from that function call, then 'result' will be void and not an error so will print "No errors".
      Using a variable in an if like this:
      if (result)
      This simply mean if the variable isn't an error. It won't matter the type, void or not.