News for hackers. Stuff that matters.
Re:Rust – How do I create a global, mutable singleton? (2015) by simion314 on Wednesday July 22, 2020 @11:26AM (#23918019)
There are cases where you only have 1 instance of something, never 2 or more. Most developers use such things dailyu, like in JS you have the window object, most of the time you don't pass the window as a parameter in all your functions but in one project I worked it was a requirement to pass the document as a param in all functions that needed it. So the my opinion is that it depends, sometimes having a World,App,Window etc global helps more then it causes issues and is preferable to a solution that is pure that costs you more. FYI I use a functional style as much as possible, no side effects but sometimes pragmatism wins, in my current project I guarantee we won't change our database or upgrade the current framework to a new cooler one.
Re:Rust – How do I create a global, mutable singleton? (2015) by estebank on Wednesday July 22, 2020 @12:36PM (#23918787)
> There are cases where you only have 1 instance of something, never 2 or more.

I've seen plenty of cases where this assumption stops being true and only through pain and sweat the design could be disentangled after the fact. On the other hand I've never been more than mildly inconvenienced by in practice global state that had an API that didn't assume it.

Writing the logic as if it could be more than one at a time also makes it much easier to write isolated tests and reduced integration tests.

I have always seen singleton designs in codebases where testing was wanting.

Re:Rust – How do I create a global, mutable singleton? (2015) by simion314 on Wednesday July 22, 2020 @01:47PM (#23919608)
As I said it depends, do you pass in js the Math object around because you assume that in future you want to replace the Math.abs function with something different ?

Sure I seen the issues you mentioned too and many other issues where the initial requirements or assumptions changed.