That both your cases match is logical and consistent as there is only one type of matching irrespective of whether it is with a =, case or any place where matching occurs. It is really not illogical that it does match as a map in any a pattern means check that it is a map and has at least the key/values specified. Otherwise you would end up with
%{abc: "bert"} = %{abc: "bert", xyz: "carl"}
failing as not all the keys are specified. Which would make the matching almost useless.
There is one simple change that should not (in principle) break any existing code, but which can spare inexperienced developers a lot of frustration:
Let structs: false be the default option for inspect/2.
Setting âstructsâ to either true or false is trivial. Being aware of its existence and recalling it from memory in the middle of a debugging frenzy is not. I had to automate the output of the inspect/2 function (using a keyboard shortcut) simply so that I do not, ever again, forget to set âstructsâ to false. Opting out of unnecessary detail makes more sense than opting in to access potentially valuable information.
We could say the same about charlists: :as_lists, but on the other hand it also can become confusing in stack traces that use charlists and/or structs.
Iâm really curios to learn examples where this was useful to you. To me, I much prefer the compact representation but itâs subjective.
Also, worth clarifying, the issue is not just showing structs as underlying maps, its more about NOT using custom inspect implementation for structs, correct?
Take the example of a changeset on which Ecto.Changeset.get_field/3 is applied: the value may be taken from âchangesâ, or from the data struct. When [structs: true], the first one is visible, the second one hidden. This is not an issue at all when you are experienced. At the beginning, however, you miss the fact that there is a struct holed in there. You receive a clue, but it is not enough to guide you. Because, unlike the (also baffling) charlists mentioned by hauleth, that clue is lost amongst a heap of other information.
I hope the above sheds enough light on the issue. Basically, for experienced users, another version of inspect/2, named in a way that would relinquish all claims to in-depth inspection (say glance/1 or peek/1), could provide the compact representation most developers likely prefer.
The problem with structs: false by default is that it returns ârawâ representation of struct, so if you will have:
defmodule Foo do
@enforce_keys [:a]
defstruct [:a]
end
%Foo{a: 1} |> inspect(structs: false)
# => %{__struct__: Foo, a: 1}
## And now is a problem
%Foo{} # This will fail as not all required keys are present
%{__struct__: Foo} # This will happily pass with invalid structure
I mean, this is not as if Enum.member? didnât existâŠ
We are used to write in for iteration in many languagesâŠ
And precisely use predicate (with question mark) methods for return a booleanâŠ
Maybe using in? in the case you illustratedâŠ
I bet that currently in is a syntactic sugar anywayâŠ
You should see some Erlang code to understand arrows have a special meaning.
Although it looks like Ruby, it comes from Erlang⊠Elixir has made a bridge for Ruby programmers to get into Erlang, but Erlang comprehension looks like this.