7 ms·
Immer is a library of persistent and immutable data structures written in C++
- saagarjha 7y agoRelated: it’d be great if C++ had const constructors, so that we could have true const objects.
- VeryHacker 7y agoThere are constexpr constructors
- saagarjha 7y agoI'm looking for constructors that return immutable objects. Is there something in C++ that can help me?
- nurettin 7y agoConstructors do not return, and return type and lhs are decoupled, so you are looking for auto const &lol = make_wtf();
- saagarjha 7y agoWell, more like I’m looking for something that gives you a const that you can’t get rid of. You could use a factory method but it’d be nicer if I could have a constructor that did it.
- munchbunny 7y agoThat's just not how C++ is designed. If you want const, you stick const on the member variables or declare member getter functions const. Then the language checks your work when you initialize an object into a constant variable. This allows you to force an object to be immutable at a compiler enforced level regardless of how the calling code tries to initialize it.
- saagarjha 7y agoUnfortunately I am trying to wrap const to something that wasn’t designed this way: https://news.ycombinator.com/item?id=20948523 https://news.ycombinator.com/item?id=20948523
- nurettin 7y agoSince soft const constraint is not part of the type, perhaps you could pass it to a lambda function that treats it as a const and another lambda function that treats it as mutable. A sort of const region and mutable region.
- leni536 7y agoWhat would be the utility of such a constructor? You can just initialize const objects with regular constructors. How are these objects not "true" const?
- saagarjha 7y agoThe specific use case I had in mind was a implicit “conversion” constructor that took an immutable object and gave back something that you couldn’t strip the const off of (it’d also take a mutable object and give you a normal thing back). What I have specifically is a class that provides a base set of operations and a subclass that lets you do some modifications, and what I am trying to do is wrap it in C++ with an object that is const depending on which class I am converting (converting object to const wrapper_class and mutable_object, a subclass of object, to just wrapper_class).
- leni536 7y agoI don't fully grasp what you are trying to do here. This looks familiar though: > What I have specifically is a class that provides a base set of operations and a subclass that lets you do some modifications If you actually do this with public inheritance then you possibly break const correctness. Having to separate types for the immutable and mutable data structures with appropriate conversions is a sound idea though.
- munchbunny 7y ago> What I have specifically is a class that provides a base set of operations and a subclass that lets you do some modifications Your object isn't actually immutable, it just doesn't let you directly mutate it. That's a really important distinction. That's a common pattern. In C++, rather than using a subclass for the mutator, use a different (friend) class. The semantics for implicit conversion between two different classes will allow you to accomplish what you are asking for with enforcing no changes from one interface but not the other.
- saagarjha 7y ago> In C++, rather than using a subclass for the mutator, use a different (friend) class. Unfortunately I don't control this class :(
- acemarke 7y agoIronically, there's a name clash with a fairly popular immutable update library for JS: https://github.com/immerjs/immer https://github.com/immerjs/immer Looks like the C++ one started first (2016-05 vs 2017-12).
- aidos 7y agoI assumed it was some sort of port, but they’re totally different things! OT but it’s worth reading the code for immerjs. It’s super small, super clever and shows the power of proxies in js.
- acemarke 7y agoYep. Michel Weststrate is a genius, and also an all-around nice guy. I hope to have a chance to meet him in person. (For reference, I'm a Redux maintainer. We now recommend Immer as the best way to write immutable update logic in Redux apps, and use it in our Redux Starter kit package.)
- akuji1993 7y agoSaw this code in a client's redux project, it was awesome to see that. So much easier than how we were about to do our very complex state updates. It takes away a lot of the complexity. Immediately copied that and showed it to our webdev channel.
- acemarke 7y agoGlad to hear it! Please let me know if you have any further feedback from using it. Here's the current roadmap towards 1.0: https://github.com/reduxjs/redux-starter-kit/issues/82 https://github.com/reduxjs/redux-starter-kit/issues/82 I just pushed out a breaking set of changes a few days ago, and will likely make one more set of breaking changes to `createSlice` in the near future. After that, I'm hoping the API will _mostly_ be good to go, and I can push it towards 1.0 in the very near future.
- k3liutZu 7y ago
- spraak 7y agoFor some perspective on why the name was probably chosen: 'immer' means 'forever' or 'always' in German
- ur-whale 7y agoCould someone provide an actual example use-case for immutable data structures in C++? The linked document only mentions high-level examples, all of which seem to be trivially implementable via mutable data structures and a bit of being careful. Does this buy anything beyond peace of mind that if you pass an immutable DS to another thread, they won't be able to change it?
- corysama 7y agoImmutable data structures are great for undo-redo. Sean Parent has a conference video out there about how undo in Photoshop is implemented by breaking the image into tiles then making a list of the history of each tile. This allows the “History Brush” to reach into the past and paint old pixels over new ones.
- a_t48 7y agoI wrote something similar for a resource system for a game engine. If you had a ResourceHandle<T> you knew that the T inside it would never change, and it was safe to pass to any other system. You could always make a mutable copy of the RH<T> to get a T out (and as an optimization if you held the last handle, skip the copy). Once you were done writing to the T, you could tell the resource system to harden it into a handle to give to other systems again (which included serializing it to give it a static hash for the T). There was other cleverness to make it ergonomic that I've since forgotten. Across a large codebase it _was_ nice to ensure that immutable data wasn't going to change, and that you could load up data with the same hash again later and get the same results. It _is_ possible to do via mutable data structures, but you can't always ensure that every person on your team will be careful in the right way.
- stabbles 7y agoThe author has created a text editor which uses immutable data structures s.t. he can do fast undo / redo [1] [1] https://github.com/arximboldi/ewig https://github.com/arximboldi/ewig
- bjoli 7y agoI saw a demonstration where he copy pasted a file into itself until it was larger than whatever memory he had in the computer. He could still efficiently edit it (the data pointers were duplicated, not the data). Pretty impressive.
- deleted 7y ago[deleted]
- siempreb 7y agoThere is something about this immutability thing I really don't like. In the authors example it looks like v1[0] is set to 42 (const auto v2 = v1.set(0, 42)), at least that's how you read it because it contains the word 'set', but you have to 'know' that v1 is immutable and is actually not setting the value, but returning it. So IMAO the naming should be better, I like to read code that is not confusing. If this construct, that I actually never needed in my entire career, is handy for undo/redo, why not just have a dead simple undo stack? In what situation is an immutable structure your best choice?
- agumonkey 7y agowhat would be a good word for this ? recreate ? recompute ? with_new ?
- physicsyogi 7y agoI'd probably be happy with "update", which is similar to Clojure's API for its persistent vector and map. https://clojuredocs.org/clojure.core/update https://clojuredocs.org/clojure.core/update
- masklinn 7y agoupdate is a different operation, it takes an update function for the value. The clojure equivalent to set is assoc.
- masklinn 7y agoClojure as `(assoc col key val)` and `(update col key fn)` though I don't know that the naming scheme is significantly clearer.
- ScottBurson 7y agoIn general, I think verbs should be avoided, when possible, as names of functional operations. For instance, in Java, 'BigInteger.add' should have been 'plus'. Compare: x.add(y); x.plus(y); People have been known to write the first one expecting 'x' to be updated. With the second, I think it's clearer that it's a value that needs to be assigned to a variable. For the operation in question here, I suggest 'with'. This is what I have used in my own functional collections libraries; I think it originated in SETL.
- rgovostes 7y agoThe Clang Static Analyzer uses immutable data structures. Here, for instance, is its immutable list implementation: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/ADT/ImmutableList.h https://github.com/llvm/llvm-project/blob/master/llvm/includ... The data structures are used during program state exploration because they are more memory efficient than storing many copies of the program state with small mutations.