Moving from here: https://github.com/numba/numba/issues/6493
I’m trying to make a knowledge base class with numba that can utilize a number of user defined types (structrefs or maybe namedtuples). This is of course challenging since numba doesn’t use dynamically typed data structures. I’m hoping to find some way to work around this though.
So my idea is to have my KnowledgeBase just hold untyped pointers to different type specific storage objects and then overload the IO of the knowledgebase so that those pointers get casted to the appropriate types given the input type. For example I might want to declare/assert facts to the knowledgebase in a njitted context.
@njit def right_hand_side_of_rule(kb,...): ... kb.declare('point1',Point(1,2)) ...
So then to make this work I would have kb.raw_pointers = Dict.empt(unicode_type,i8) and do something like:
@overload(KnowledgeBaseType.declare) def kb_declare(...): #some type stuff is resolved typ_str = .... storage_object_type = ... def impl(...,name,x): storage_object = _cast_ptr_to_obj(kb.raw_pointers[typ_str],storage_object_type) storage_object.declare(name, x) return impl
I noticed that a lot of numba types have a meminfo object. Is there any way to write a _cast_ptr_to_obj(ptr,obj_type) that just takes in the meminfo.data as an integer and pops out a structref (or Dict, List, etc.) that can be used in an njitted function?