# Check values on adding to Dict

**URL:** <https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410>\
**Category:** General Usage\
**Tags:** question, setindex\
**Created:** [August 6, 2022, 7:23pm UTC](https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410 "2022-08-06T19:23:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)\
**Post date:** [August 6, 2022, 7:23pm UTC](https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410/1 "2022-08-06T19:23:00Z")

</div>

Apologies if this should have been obvious, but I could not find a discussion or documentation on the topic. Admittedly, its late, I’m tired, and I may just be overlooking things…

The question I’m struggling with is essentially a validity check when adding items to a `Dict`.

I have a `Dict{String, Stream}`, where `Stream` is a struct that represents a stream in my process - literally a pipe with stuff flowing in it. Each `Stream` in turn references a component list, which is a `Dict{String, Component}`. Here the `Component` type is a struct referring to a specific molecular species.

I would like to ensure that all the streams refer to the same component list. In my own code, this is easy - I just have a single master component list. However, I would like to eventually turn this code into a package, and then I have no guarantee that end-users will do the same. I would therefore like to check that, when a stream is added to the master stream list - the `Dict{String, Stream}` mentioned above - that the stream uses the same component list - the `Dict{String, Component}` - as all the streams already in the list.

If anyone has an idea how to do this, I would appreciate some pointers. Should I override `setindex!()`, or is there something better I should look at? Overriding `setindex!()` (if that is even possible) feels like there would be creative ways to tie my own shoelaces together if I take this path.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 6, 2022, 9:19pm UTC](https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410/2 "2022-08-06T21:19:54Z")

</div>

I am not sure if I understand your problem. It seems that in your code you use a `Dict` object (a type from `Base`) to execute some task, why are you sure the user will also use a `Dict` to solve any of their problems and why do you fell the need to restrict how they use a `Base` type (`Dict`) when it is storing elements of a type that comes from your package? Is this `Dict` a global object instanced when the package is loaded? If not I do not understand what design are you trying to attempt here.

---

<div class="post-metadata">

**Author:** ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)\
**Post date:** [August 7, 2022, 7:42am UTC](https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410/3 "2022-08-07T07:42:32Z")

</div>

In general terms, when adding an entry to the `Dict`, I want to be able to first check the value to see if it is valid. For a simple example, if the code is

`sysstreams["Feed"] = X`,

I want to first check the contents of X before the `"Feed" => X ` pair is added to `sysstreams` and throw an exception if it is invalid.

I suspect I am simply going to have to add a function `addstream` and do the check in there, rather than allow direct additions to the `Dict`. A bit more verbose, perhaps, but probably much simpler than intercepting `setindex!`.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [August 7, 2022, 8:27am UTC](https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410/4 "2022-08-07T08:27:06Z")

</div>

As far as I see it, you would want to expose an API:

1. Either via some functions `new_stream_registry` and `addstream` as you suggested.
2. Or, defining your own type `StreamRegistry` with custom methods on `getindex` and `setindex!`.

In any case, that a `Dict` is used internally is an implementation detail and should be hidden. The second way might be nicer syntax wise, but I would only use it if `StreamRegistry(...) isa AbstractDict` in any reasonable sense.

---

<div class="post-metadata">

**Author:** ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)\
**Post date:** [August 7, 2022, 8:36am UTC](https://discourse.julialang.org/t/check-values-on-adding-to-dict/85410/5 "2022-08-07T08:36:15Z")

</div>

I rather like the idea of your second proposal. Will try that today. Thanks!
