Awlexus

Awlexus

Warning about missing pattern match, despite pattern matching

I’m getting the following warning when compiling my project and I’m not sure how to resolve it, because from what I see there is a proper pattern match

Warning
    warning: a struct for Plug.Upload is expected on struct update:

        %Plug.Upload{image | filename: random_filename(name)}

    but got type:

        dynamic()

    where "image" was given the type:

        # type: dynamic()
        # from: my_file.ex:95:79
        %{"image" => %Plug.Upload{filename: name} = image} = params

    when defining the variable "image", you must also pattern match on "%Plug.Upload{}".

    hint: given pattern matching is enough to catch typing errors, you may optionally convert the struct update into a map update. For example, instead of:

        user = some_function()
        %User{user | name: "John Doe"}

    it is enough to write:

        %User{} = user = some_function()
        %{user | name: "John Doe"}

    typing violation found at:
    │
 96 │     image = %Plug.Upload{image | filename: random_filename(name)}
    │             ~
    │
    └─ my_file.ex:96:13: MyModule.put_random_filename/1

Code
  defp put_random_filename(%{"image" => %Plug.Upload{filename: name} = image} = params) do
    image = %Plug.Upload{image | filename: random_filename(name)}
    %{params | "image" => image}
  end

  defp put_random_filename(params), do: params
Elixir -v

Erlang/OTP 28 [erts-16.1.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.19.2 (compiled with Erlang/OTP 28)

Different variations that trigger the same warning

No match against file name
  defp put_random_filename(%{"image" => %Plug.Upload{} = image} = params) do
    image = %Plug.Upload{image | filename: random_filename(image.filename)}
    %{params | "image" => image}
  end
Flipping the assignment
  defp put_random_filename(%{"image" => image = %Plug.Upload{filename: name}} = params) do
    image = %Plug.Upload{image | filename: random_filename(name)}
    %{params | "image" => image}
  end

Marked As Solved

sabiwara

sabiwara

Elixir Core Team

This actually looks like a bug (or limitation) when inferring non-atom map keys.

I opened an issue Structs nested in domain-key maps are not inferred · Issue #14915 · elixir-lang/elixir · GitHub.

Also Liked

a-maze-d

a-maze-d

image = %Plug.Upload{image | filename: random_filename(name)}

What about rewiting this to:

image = %{image | filename: random_filename(name)}
garrison

garrison

Surely this is a bug? The struct is clearly typed. Maybe someone with more knowledge will reply, but I would say file an issue.

Does this infer properly?

def foo(%{"image" => image}) do
  %Plug.Upload{} = image
  %Plug.Upload{image | filename: "foo"}
end
Awlexus

Awlexus

I’ve tried these 2 variations. Both trigger the warning

  defp put_random_filename(%{"image" => image} = params) do
    %Plug.Upload{filename: name} = image
    image = %Plug.Upload{image | filename: random_filename(name)}
    %{params | "image" => image}
  end

  defp put_random_filename(%{"image" => image} = params) do
    %Plug.Upload{} = image
    image = %Plug.Upload{image | filename: random_filename(image.filename)}
    %{params | "image" => image}
  end

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement