containers
containers copied to clipboard
Can Hashmap be used in @nogc code?
I thought that attribute inference would let HashMap be @nogc when using Mallocator?
Error: @nogc function 'dplug.gui.font.Font.getGlyphCoverage' cannot call non-@nogc function 'containers.hashmap.HashMap!(GlyphKey, Image!(Color!(ubyte, "l")), Mallocator, generateHash, true).HashMap.opBinaryRight!"in".opBinaryRight'
I think this was caused by attribute inferrence not working on the various functions in HashMap. The problem is that the @nogc
-ness of the HashMap depends on the hash function also being @nogc
.
One can get around @nogc attributes by casting. Making the hash function(s) predicates with defaulting to the built in types might be the way to go?
Druntime should offer @nogc
hashing for basic types. Structs and classes will work if they implement toHash() @nogc
. Also all the container methods using hashes like insert(Key, Value)
would have to become templates for attribute inference to kick in.
I've just hit the same issues when implementing a hash table. For now I'll use a separate hashOf()
function that reimplements hashing of basic types.
@Stretto Predicates for the hash function are a good idea. It is just not quite so obvious to have as the less comparator in sorted lists. There may be many ways in which one thing is greater than another, but typically only one way it equals another.
I don't think this is practical right now in D. We need to select the attributes (with/without @nogc
) depending on the allocator and the supportGC
parameter, which is currently doable only by duplicating all function declarations. Inference for template functions is usable but only for methods and not e.g. destructors.