Dictionaries.jl
Dictionaries.jl copied to clipboard
How to construct an empty AbstractDictionary given the Dictionary Type?
Hi, sorry if this question is somehow inbetween an discourse question and an issue/feature request.
Is it possible to create a an empty AbstractDictionary given the concrete type of the system?
I am asking because it seems within AbstractDictionaries/AbstractDict it is especially common to have totally different kind of constructors which hence do not provide a common interface.
I see that I can construct empty AbstractDictionary, given an instance, but I wasn't able to find one which takes the type instead.
I have only recently started to formalize this with empty_type. This is the current set of "rules":
- Dictionaries that are
isinsertablehave a zero-argument constructor. E.g.Dictionary{String, Float64}(). - Dictionaries that are
issettablehave have anundefconstructor likeDictionary{String, Float64}(new_indices, undef). - Dictionaries designed to store data (including the above two, or even immutable dictionaries, but not the wrapper-like dictionaries returned from
view,filterview,reverse, etc) would have a two argument constructor likeDictionary{String, Float64}(new_indices, new_values). There would generally be a one-argument constructorDictionary{String, Float64}(from)which is just sugar forDictionary{String, Float64}(keys(from), values(from)). Thevalues()is there to supportDict, but otherwise it's an identity function.
When you have a new keytype or eltype that you want to use, there are helper functions. Insertable dictionaries can be created by empty and you can discover their types via Dictionaries.empty_type and call the zero-argument constructor from that. Settable dictionaries can be created by similar and I'd like to introduce a similar_type function. We also need a type-domain function for 3 above, for when you already know the keys and the values; I don't have a good name for that (for reference, this would be the functional equivalent of StaticArrays.similar_type which was designed around immutable arrays).
Does that help? I'd like to finish this and write it all up at some point.
yes that helps - I think for my usecase it is fine to fallback to zero-argument constructor for now.
Looking forward to the whole type interfaces!