[erlang-questions] Matching Empty maps

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Tue Feb 7 16:23:55 CET 2017


The alternative notation is something along the lines of:

#{ ... }

which says that we have a map with at least one extra undefined element. In
that case, the pattern

#{ a := _, ... }

would say "the key a MUST be present, I don't care about the value, and
there MUST be additional elements in the map", whereas

#{ a := _ }

is *exactly* the one-element map. Too late for this notation though.

On Tue, Feb 7, 2017 at 3:48 PM Raimo Niskanen <
raimo+erlang-questions@REDACTED> wrote:

> On Tue, Feb 07, 2017 at 12:45:15PM +0100, Joe Armstrong wrote:
> > Ten minutes ago I was happily recursing down a list and discovered
> > to my horror that an empty map isn't an empty map.
> >
> > 1> #{} = #{a => 1}.
> > #{a => 1}
> >
> > The documentation actually says:
> >
> > <quote>
> > #{} = Expr
> >
> > This expression matches if the expression Expr is of type map,
> > otherwise it fails with an exception badmatch.
> > </quote>
> >
> > So #{} is an empty map in an argument position but not an empty map
> > in a pattern.
> >
> > So this is not a bug since the system does what the documentation says
> > but it is very weird.
> >
> > Is there some deep reason for this that I've missed ??????
> >
> > Very Puzzled
>
> I think it is quite logical since #{a := _} in a match position matches a
> map with a key 'a' plus any number of other keys in it, so why should not
> the empty map #{} in a match position also match any number of other keys
> in it?
>
> >
> > /Joe
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170207/23992d73/attachment.htm>


More information about the erlang-questions mailing list