Check values on adding to Dict

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.

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.

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!.

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.

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