hst337
Deprecate map.field() syntax suggestion
iex> map = %{x: 1}
iex> map.x
1
iex> map.x()
1
Why would anyone use the map.x() syntax for getting map value? I’d suggest deprecating it since it is misleading. Elixir compiler can add IO.warn to map clause when this happens in runtime
Most Liked
josevalim
@hst337 it is already deprecated when we detect it is a map at compilation time:
Interactive Elixir (1.15.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule Foo do
...(1)> def bar(map) when is_map(map) do
...(1)> map.foo()
...(1)> end
...(1)> end
warning: incompatible types:
map() !~ atom()
in expression:
# iex:3
map.foo()
where "map" was given the type map() in:
# iex:2
is_map(map)
where "map" was given the type atom() (due to calling var.fun()) in:
# iex:3
map.foo()
HINT: "var.field" (without parentheses) implies "var" is a map() while "var.fun()" (with parentheses) implies "var" is an atom()
Conflict found at
iex:3: Foo.bar/1
We have been planning on adding a runtime deprecation for a while and there is an open issue tracking this. ![]()
hst337
module.function(1, 2, 3) # Is a call
module.function(1) # Is also a call
module.function() # Is a call and also a map lookup
This is the “misleading” I am talking about
It is impossible to know for sure, but compiler is already able to check the types and warn when map.field() is called when map is a map. Plus, runtime warning is okay too.
Great! I am sorry, I didn’t check the issue tracker
zachallaun
Isn’t this just a constraint of parsing/syntax? In your example, it’s not possible to know whether map.x() is a key lookup on a map, or a function call on a module, until runtime. I don’t believe this could ever be a compile-time warning. (I also generally disagree that it’s misleading/etc. even though I agree that map.x() is uncommon.)
Popular in Proposals: Ideas
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









