hst337

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

josevalim

Creator of Elixir

@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. :slight_smile:

hst337

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

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.)

Where Next?

Popular in Proposals: Ideas Top

sbennett33
When building a component library, it is often useful to give users the ability to customize the underlying element or component to use. ...
New
DaAnalyst
@chrismccord @josevalim @steffend Been using colocated JS for a while and I keep on stumbling on the same limitation: there is no way (a...
New
skyqrose
Proposal: I wish the socket connect/3 callback could allow returning a reply to the frontend, which would then be visible in phoenix.js ...
New
wceolin
I have two proposals - not sure if I should split them into different threads but I think they’re related: Add a new JS.call/2 function...
New
kccarter
This is likely a feature request unless we’re overlooking something, but it would be a nice improvement to the developer experience if th...
New
spicychickensauce
I’ve previously explored what is possible today with hacks to implement view transitions in our apps: I have since created a fork to im...
New
i-n-g-m-a-r
When debug_errors: true javascript can be executed. <html> <body> <form id="xss" action="https://domain.com/NateMai...
New
knoebber
I started a new phoenix project with --no-tailwind and found friction: delete tailwind classes from core components delete tailwind cla...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
New
Aduril
Hello there, Whenever I setup a new project, there is a small function I always add: reply/1. What does it do? In a LiveView mount, han...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement