Hacker News new | ask | show | jobs
by leeoniya 1452 days ago

  $a(); // read
  $a.set(20); // write (1)
  $a.update((prev) => prev + 10); // write
why not $a(20) and $a(prev => prev + 10)?
3 comments

It may be as a safety measure, it's much less likely to misuse the setter if it's divided into two different functions. If it's just one function you need to constantly ask yourself "wait, am I setting a function or not?", unless you always want to write `$a(() => value)`, which would be pretty ugly.
I started with that but then I thought about what if a callback or some other function is stored as an observable (i.e., like `useCallback`). Also, as fabiospampinato mentioned it's a cleaner seperation of get/set which makes it much easier to differentiate at a quick glace. You can't easily mess it up. I'm still debating whether to achieve what you're after. Maybe I have something mentally twisted and it's easier than I think.
I agree, this would make it a lot cleaner.