# Creating dicts in foreign C code

**URL:** <https://swi-prolog.discourse.group/t/creating-dicts-in-foreign-c-code/1423>\
**Category:** General\
**Created:** [October 24, 2019, 8:07am UTC](https://swi-prolog.discourse.group/t/creating-dicts-in-foreign-c-code/1423 "2019-10-24T08:07:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![likelion](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/likelion/32/57_2.png) [@likelion](https://swi-prolog.discourse.group/u/likelion)\
**Post date:** [October 24, 2019, 8:07am UTC](https://swi-prolog.discourse.group/t/creating-dicts-in-foreign-c-code/1423/1 "2019-10-24T08:07:03Z")

</div>

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?):

```nohighlight
#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?

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [October 24, 2019, 3:41pm UTC](https://swi-prolog.discourse.group/t/creating-dicts-in-foreign-c-code/1423/2 "2019-10-24T15:41:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 25, 2019, 2:02am UTC](https://swi-prolog.discourse.group/t/creating-dicts-in-foreign-c-code/1423/3 "2019-10-25T02:02:36Z")

</div>

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.

> [@likelion](#):
>
> BTW, why `SWI-Prolog.h` does not define ATOM\_dict

Because there is very little useful you can do with it 🙂
