Yeah, that would work.
I guess my main worry, is that it's nice to have simple APIs that can operate with whatever value you give it, and it just works. You don't have special-case behaviors for special values.
It's one thing I dislike about React's useState() hook, which looks like this:
const [state, setState] = useState(...)
useState()
returns a tuple containing the current state, and a function to update the current state (the setState()
function). You can call setState()
with whatever value to make that the new current state.
With one exception. If you call it with a function, instead of setting the current state as a function, it'll call your function, passing in the old state and expecting you to return a transformed state. This is useful and important behavior, but it's also really annoying that they're overloading the same useState()
function to provide it. What if, I want to actually store a function as state, or, I want to store a user-supplied value (I don't care what it is, could be a function, or anything else) as state? This has happened to me before. Well, you can't, because of how this function is set up. You instead have to put the function into a single-property object and store that as the state.
The root of this problem is that they tried to smartly overload a single function to have two different behaviors depending on what was passed in. I worry that creating a "lazy function" will be encouraging many APIs to make this same mistake, where they overload their functions to have two different behaviors depending on what was passed in, and now you can't just use those APIs with whatever random user-supplied value that you received, you have to be cautious because it's possible that user-supplied value is a lazy function, which might trigger special behaviors in the function you're trying to use, and you might not want those special behaviors to be triggered, but there's no good way to opt-out of that.
e.g. I can't just call map.getOr()
with some random user-supplied value I received as input, unless I'm ok with map.getOr()
's default treatment of how it handles lazy functions, which it's possible that that's not how I want it to behave.