Pick the fix
A file wrapper must never be copied, so the descriptor can't be closed twice. Which design?
Hint 1 of 1
Which option makes copying impossible?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A struct File: ~Copyable with deinit { close(fd) } and consuming func close()
Correct. One owner, and deinit runs exactly once.
Why not the others?
- B A final class with deinit. It works, but references can be shared freely, so ownership is unclear.
- C A struct with an
isClosedflag. Each copy has its own flag, so two copies can both close. - D An enum with an open and closed case. Copies are still allowed and can disagree.