by hackyhacky · 1y ago · 0 comments
In this use case, my instinct tells me that trait specialization is the wrong tool for the job. The author is trying to dispatch different functions based on a flag set in the caller. I can think of two more elegant ways to do this:
* Make the file system itself a trait, with implementations for read-only and read/write. Pass the concrete implementation as a parameter to the function using it.
* Store the capabilities as a variable in the file system object and query it at runtime. This can be done with an if.
Sometimes the simpler approach is better.
> Make the file system itself a trait, with implementations for read-only and read/write
Those are separate concrete implementations between read-only and read-write; a write operation can be implemented on the read-write implementation only.