Creating dicts in foreign C code

Is there a preferred way to efficiently create dicts in foreign C code?

Of course, I can create a list of pairs and call dict_pairs/3, or construct a compound term (BTW, why SWI-Prolog.h does not define ATOM_dict?):

#define ATOM_dict (_PL_atoms()[2])

functor_t fDict = PL_new_functor(ATOM_dict, 3);
term_t tTag = PL_new_term_ref();
term_t tVal = PL_new_term_ref();
PL_unify_integer(tVal, 42);
term_t tKey = PL_new_term_ref();
PL_unify_chars(tKey, PL_ATOM, 5, "mykey");
term_t tDict = PL_new_term_ref();
PL_cons_functor(tDict, fDict, tTag, tVal, tKey);

But should there be a better way?

I was looking for this recently, could not find a better way either.

As is, this won’t work as it doesn’t provide the right ordering of the keys. This requires a dedicated API. This has been on the agenda as long as dicts exists, but it never materialized.

Because there is very little useful you can do with it :slight_smile:

1 Like