Open Xyphenore opened 1 year ago
@Xyphenore Just to make sure I understand you correctly, do you want map objects to behave like (I'm using int
as a placeholder for any non-cv-qualified type):
const std::map<int, int>
(entire map is read-only), std::map<int, int>
(only the keys are read-only, but can be added/deleted), std::map<int, const int>
(keys and values are read-only, but can be added/deleted),In theory the second option should be how the map objects already behave (although I will admit I'm not sure we have explicitly tested that this is the case; if it is not I would consider that a bug that needs fixed).
Regardless, I think this is a great suggestion. Const-ness/immutability is something that is missing from CMakePPLang in general. We've been modeling CMakePPLang after Python so I'd be inclined to, as a first step, introduce a Tuple
class which is immutable once created. With a Tuple
object we could make a FrozenMap
class pretty easily.
The first thing 'const std::map<int, int>' is what I need. This map contains keys and values, but it does not allow adding or removing values, similar to the FrozenMap object in Python.
Great job on this project! I've been searching for something like this to enhance my CMake scripts and incorporate a map container. It's unfortunate that your work hasn't gained enough popularity yet.
Problem description In Python, there is often a need to create immutable maps and lists that prevent the addition or removal of values. I use maps to store constants such as file hashes, which should remain unchanged during execution. However, currently, I am able to modify the map containing file hashes. I would like the map to be constant, similar to how it behaves in C++.
Requested solution I suggest adding a member function to freeze an object, specifically a map. When an object is frozen, the 'set' function should throw an exception and prevent any modifications to the object.
Considered alternatives One alternative I've considered is using a keyword in the constructor to directly create a frozen map without the need for a separate 'freeze' function.
Note: Please ensure that the implementation also accounts for thawing or unfreezing an object if needed.