4 ms·
You don't need RAII to manage non-memory resources. All you need are first class functions.
by orlandu63 14y ago
You don't need RAII to manage non-memory resources. All you need are first class functions.
- anonymousDan 14y agoCould you expand on this a bit? I'd be interested to see an example of what you mean.
- zem 14y agoit's a ubiquitous pattern in ruby. this blog post uses a file open/close lifecycle manager as an example: http://yehudakatz.com/2012/01/10/javascript-needs-blocks/ http://yehudakatz.com/2012/01/10/javascript-needs-blocks/
- Evbn 14y agoThat's more about coroutines than first class functions, and it is RAII, using a wrapper methodd for the actor/dtor instead of defining a class.
- zem 14y agoit is not raii as i understand the term. there is no destructor involved in closing the file, there is just a function that opens the file, passes your code the handle, then closes it when you're done.
- gsg 14y agoThe function trick suffices to handle objects with stack-shaped lifetime (certainly a very common case), but it does not suffice when resources are associated with parts of a data structure. The strength of RAII is that it handles both of these cases with the same machinery.