containers icon indicating copy to clipboard operation
containers copied to clipboard

Can Hashmap be used in @nogc code?

Open p0nce opened this issue 8 years ago • 4 comments

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'

p0nce avatar Aug 04 '16 13:08 p0nce

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.

Hackerpilot avatar Aug 04 '16 19:08 Hackerpilot

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?

Stretto avatar Aug 25 '16 20:08 Stretto

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.

mleise avatar Sep 09 '16 12:09 mleise

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.

CyberShadow avatar Sep 18 '21 10:09 CyberShadow