accessible
accessible copied to clipboard
How does this differ from Map's implementation?
I was looking for exactly that, but I wish the doc explained why the implementation is more complex than expected and why we shouldn't simply use defdelegate to Map, as in #5?
I can only speculate. Let's have a look at put:
def put(struct, key, val) do
if Map.has_key?(struct, key) do
Map.put(struct, key, val)
else
struct
end
end
This is nearly Map.put, but doesn't put a value if it is not declared in the struct. Which makes some sense, because you'd want the struct to keep its structure, and not have additional ad-hoc keys. One can argue whether it should silently discard (as is done here) or throw an exception, and I agree that such a choice should be documented.