How about predefined dict function Dict.get(Key, Default)

Implementing it myself works, but forces me to use a specific tag for the dict.

M.get(Key, Default) := Val :-
    ( Val = M.get(Key), !
    ; Val = Default).

get_dict/4 doing ~the same would IMO also be a good addition.

Or did I just miss some convenient shortcut / already present way to do this >

As is, tags are tight to modules when using user defined functions on dicts. I have my doubt on whether you want to allow for functions on dicts regardless of the tag. That would eliminate all modularity.

So, I’m afraid the answer is no. Of course you can hack in the implementation :slight_smile:

P.s. this is teh second time in a relatively short period that I see (A, !; B). Where does this come from, rather than the normal (A->true;B)?

P.s. One should avoid unification of arguments before a ! or ->. As is, if the dict has a value different from the default but you ask it with the output instantiated to the default it will succeed. Use

M.get(Key, Default) := Val :-
    (   Val0 = M.get(Key)
    ->  Val = Val0
    ;   Val = Default
    ).

I must admit that Foo = Dict.get(foo, default) is nicer than (Foo = Dict.get(foo) -> true ; Foo = default), especially because gets can be chained. Isn’t this use case common enough to grant addition of get/2 (and a matching get_dict/4) to the library?