[erlang-questions] Best way to test if empty dict/set/etc.?

Jonathan Leivent jleivent@REDACTED
Fri May 31 17:33:43 CEST 2013

On 05/31/2013 11:19 AM, Fred Hebert wrote:
> Most of these data structures have a 'new' or 'empty'  function call
> that creates an empty/blank data structure.
> I then just compare things like MyDict =:= dict:new() to know if it's
> empty. I'm not aware of a case where this doesn't work, but it makes a
> very subtle assumption that *might* break opacity: it assumes that the
> data structure doesn't keep internal statistics or traces of content it
> has seen before but is no longer there.

I wasn't sure about whether the equality test would always work, either. 
  Does the documentation ever mention whether or not equality tests on 
opaque structures is expected to work?

However, for sets/ordsets, maybe this is both O(1) and opaque:

   is_empty(S) -> sets:is_subset(S, sets:new()).

-- Jonathan

More information about the erlang-questions mailing list