5 ms·
Stop caring about OO patterns. Inheritance and the stuff that came with it during that era was a boondoggle. Arc<RwLock<T>> fits the bill. Just pass it around.
by echelon 6y ago
Stop caring about OO patterns. Inheritance and the stuff that came with it during that era was a boondoggle.
Arc<RwLock<T>> fits the bill. Just pass it around. Additional plumbing isn't the end of the world.
- Fiahil 6y agoIn most cases, additional plumbing is not even necessary, as it's often tied to a single object instantiated in the main function (database client, connection pool, config map, ...). The Arc<RwLock<T>> would go in that object.
- eximius 6y agoWhy is Arc needed? It's a global singleton so we don't need to worry about dropping it. Just a RwLock<T> should be fine or maybe even Cell if you don't need RwLock guarantees.
- Fiahil 6y agoArc is there if you need to send that singleton between threads. If your program is a single-thread non-async one, you can drop both Arc and Mutex, leaving you with just a regular data field :)
- steveklabnik 6y agoIf you really need something that's only for a single thread, then using TLS is gonna be better than a static. (I actually want to write a blog post about this... haven't done so yet though. People reach for static too often and thread_local! too little, IMHO. Of course, not needing either is best.)
- bluejekyll 6y agoTLS has many of the same downsides of most global state. It does remove the concurrent access issues (as long as it is only used inside a single thread), but it still creates a testing problem if access to it is not restricted. That is, don’t make globals publicly accessible, just like you wouldn’t make all data in an object publicly accessible.
- steveklabnik 6y agoAbsolutely, I'm not saying that people should be reaching for global state more, I'm saying that if you really need global state, reaching for TLS over a static is often the right call, because you're minimizing the global-ness, which drops some of the type system restrictions. That is, too many people skip straight to static without even considering TLS, not that they should be using TLS more often in general.
- bluejekyll 6y agoYes. Absolutely. TLS is definitely the preferred pattern in this case.
- cesarb 6y agoTLS has the additional downside that it makes it harder to move part of the processing to another thread, because the other thread will have a different TLS state. That is, once you have TLS, x() and thread::spawn(x).join() might no longer do the same thing. Using TLS creates thread affinity when one might not be expecting it. (I've seen way too many times a thread-local being used as a way to smuggle extra parameters, often through several layers; inevitably, this breaks when one of the intermediate layers decides to run things in a separate thread pool.)
- pandalicious 6y agoFor anyone else that’s confused, TLS = thread local storage
- steveklabnik 6y agoYes, sorry, I figured that in this thread (eyyyyyy) it would be unambiguous, but probably should have elaborated anyway. Thanks.
- deleted 6y ago[deleted]
- eximius 6y agoArc is still only necessary if your T has interior mutability or some other reason to not be Sync.
- jupp0r 6y agoOP mentioned that T is mutable
- betenoire 6y agoInheritance and the stuff that came with it isn't the end of the world.
- smt88 6y agoI have inherited old Java, PHP, and C# code bases with more than a thousand classes, lots of inheritance, and no composition. It can be so difficult to answer the question, "What happens when I call this method on this instance?" that a 10-min edit takes a full day. The code is basically frozen for fear of breaking something At some point, you see enough of this that you realize the tool is more dangerous than helpful. The makers of Kotlin and many other newer languages seem to know this and have disabled inheritance as a default or left it out entirely.
- vsareto 6y agoI've seen C# interfaces and base classes that were committed years ago and never got a second implementation. This essentially makes the interface and base class a pointless ritual in hindsight. It's always easy to spot in hindsight, but TBH I think the threshold for creating interfaces or base classes should be a bit higher - likely when you know you will need 3 or more implementations. It's always felt like premature optimization to me, unless something is hard coded to only accept an interface like using Type.IsInterface or something. Concepts like "Product" are almost always going to have multiple implementations though, so it's easy to judge in that case to make an interface/base class at the beginning. The real magic is guessing what other concepts satisfy this requirement.
- rickmode 6y agoI had this same belief but there is one other useful situation where an interface plus just one implementation class helps, and that's when you want to hide the noise of implementation details from another part of the program. This can happen between different layers of app. For example you might have a persistence layer under an API or UI, and want to expose a nice "clean" interface to the API / UI layer.
- rumanator 6y ago> Stop caring about OO patterns. A singleton is not a OO pattern, it's a memory access pattern. It's high time people stop parroting cliches and start to look at patterns as what they are: typical solutions to meet recurring requirements.
- stagger87 6y agoIn the spirit of correcting people, a singleton is absolutely an OO pattern, which doesn't mean it can't also be a "memory access pattern" (in some sick twisted meaning of memory access pattern). I mean, it was brought to fame by the GoF OO design book.
- rumanator 6y agoNo, a singleton is not a OO pattern. At all. Quoting GoF does nothing to refute that fact. A singleton is just a way to ensure a single object is instantiated and exists system-wide. That's it. It's a global object, attached to a negligible (and thus often ignored) requirement to not allow instantiating other objects of the same type. One way to implement singletons is to get a static function return a static variable. No OO required. It's all about memory access. Single variable used to store global state. That's it.
- stagger87 6y agoI appreciate your response. Unfortunately, I don't feel you have presented a convincing argument against its established classification as an OO design pattern for the last several decades. Additionally, "memory access pattern" does have an established definition (which you may also be opposed to), and you may confuse people with your usage of it. https://en.wikipedia.org/wiki/Memory_access_pattern https://en.wikipedia.org/wiki/Memory_access_pattern
- rumanator 6y ago> I appreciate your response. Unfortunately, I don't feel you have presented a convincing argument against its established classification as an OO design pattern for the last several decades. The argument is self-evident. A singleton does not use or need any OO construct to be implemented or used. There is nothing special or specific to OO to exclusively own the concept of ensuring a single globally-accessible system-wide data structure. As I've said, singletons are implemented with static functions returning static variables. No OO involved at all. Your only argument tying singletons to OO is the fact they are covered in GoF. However, that argument has no merit not makes any sense. GoF does not specify what is and is not OO. It only shows basic design patterns and presents ways of implementing them with OO languages. Otherwise you would also claim that instantiating variables is OO just because GoF describes factory methods.