If you were designing a standard library of a high level language (like Rust, C++, etc.) what would it do if while getting the current time of day (e.g. SystemTime::now()) it encountered a failed kernel system call (e.g. to clock_gettime) and why?

What do you think the existing implementations do?

  • Return error or raise exception
  • Return 0
  • Return undefined values (like stack garbage)
  • Panic/crash/segfault
  • Eager Eagle@lemmy.world
    link
    fedilink
    English
    arrow-up
    7
    ·
    edit-2
    19 days ago

    you should do what’s idiomatic for that language, e.g.: return an error in Rust; raise an exception in Python or Java; return an integer != 0 in C; etc.

    Panic/crash/segfault

    probably the worst option for a library

    • senkora@lemmy.zip
      link
      fedilink
      arrow-up
      5
      ·
      19 days ago

      +1. If your library makes it impossible to recover from errors, then it will be unusable from any program that requires recovering from errors.

      • Eager Eagle@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        19 days ago

        If it’s garbage as in a string with a bunch of information that is hard to parse, yeah, crashing without giving the caller a way to avoid it might be worse. But what is exactly this garbage and in what cases are you considering returning it at all?

        • hades@programming.devOP
          link
          fedilink
          arrow-up
          1
          ·
          18 days ago

          Uninitialized automatic variables. E.g. (in C/C++):

          int get_time() {
            int time;
            syscall(/* something that fails */, &time);
            return time;
          }