# Dictionaries and polymorphism

**URL:** https://swi-prolog.discourse.group/t/dictionaries-and-polymorphism/2332
**Category:** Help!
**Created:** [May 14, 2020, 8:17pm UTC](https://swi-prolog.discourse.group/t/dictionaries-and-polymorphism/2332 "2020-05-14T20:17:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)
#### Post date: [May 14, 2020, 8:17pm UTC](https://swi-prolog.discourse.group/t/dictionaries-and-polymorphism/2332/1 "2020-05-14T20:17:59Z")

</div>

I’m looking for pointers on how to achieve the following behaviour. I want to have a dictionary that has a key `type` and an atom value denoting the “implementation module”. Now, in the dictionary tag module, I would define a function that needs to do some kind of dynamic dispatch - first check if `type:function` is defined, if yes, call it - and if not fall back to a default implementation.

Example:

`inventory{type: book, ...}.url()` would look for `url` function in the `book` module first and call it if it’s defined, otherwise just call some default implementation.

I know that pce offers objects and inheritance but I don’t need OOP-like state, as the state is already in the dictionary.

---

<div class="post-metadata">

### Author: ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)
#### Post date: [May 14, 2020, 8:58pm UTC](https://swi-prolog.discourse.group/t/dictionaries-and-polymorphism/2332/2 "2020-05-14T20:58:35Z")

</div>

I think I got something working using `wrap_predicate/4`!

```prolog
subtype(Name/Arity) :-
  functor(Head, Name, Arity),
  Head =.. [_|Args],
  append(_, [Dict, _Value], Args),
  wrap_predicate(Head, subtype, Wrapped,(
    ((get_dict(type, Dict, M2),predicate_property(M2:Head, defined)) -> call(M2:Head) ; call(Wrapped))
  )).

```

Now I can just call use `:- subtype(url/2)` to get the desired behaviour.

---

<div class="post-metadata">

### Author: ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)
#### Post date: [May 14, 2020, 11:58pm UTC](https://swi-prolog.discourse.group/t/dictionaries-and-polymorphism/2332/3 "2020-05-14T23:58:09Z")

</div>

I think you should be able to define a module called `inventory`, with a url predicate in it, using the [user defined operations on dicts](https://www.swi-prolog.org/pldoc/man?section=ext-dict-user-functions).

---

<div class="post-metadata">

### Author: ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)
#### Post date: [May 15, 2020, 7:55am UTC](https://swi-prolog.discourse.group/t/dictionaries-and-polymorphism/2332/4 "2020-05-15T07:55:09Z")

</div>

@jamesnvc I wanted the `url` predicate in `inventory` module to actually call `url` in `book` (or whatever is the `type` key of my `inventory` dictionary holds) if defined - for a very simple inheritance-like behaviour. That’s why I did the wrapper solution above.
