DidactMacros

DidactMacros

Clarification on Question/Exercise regarding extention of protocols

I’ve been working my way through the Protocols chapter of Programming Elixir, and I came across this exercise in the last section of the chapter:

In many cases, inspect will return a valid Elixir literal for the value being
inspected. Update the inspect function for structs so that it returns valid
Elixir code to construct a new struct equal to the value being inspected.

I went ahead and implemented something:

defimpl Inspect, for: Map do
  def inspect(%{__struct__: module} = struct, opts) do
    tents = Map.delete(struct, :__struct__) |> Enum.to_list
    quote do
      defmodule unquote(module) do
        defstruct unquote contents
      end
    end
    |> Macro.to_string
  end
end

I literally threw this together after writing a small list of steps to be taken based on my understanding of the question, I’ve not assessed the veracity of the inner function here, there is likely a better and faster function than the one I’ve defined but I think the overarching problem is that my scope is too large.

The problem I’m having is that this implementation unfortunately seems to rewrite the entire Inspect.Map implementation module, which messes up a lot other functions and basically makes inspect unusable for my session.

I hoped my implementation would just override the matching function clause, and thus change the way inspect handles structs, but it is instead changing everything about how it handles maps, save for any fallbacks.

I’m also starting to wonder If I’ve understood the exercise itself. Maybe I’m meant to take a more limited approach?

I would really appreciate some guidance.

Marked As Solved

DidactMacros

DidactMacros

Using

defimpl Inspect, for: Any instead of defimpl Inspect, for: Map

fixes the issue.

Also I edited my OP message to scope the args to Map.delete due to the pipe ( |> )

iex(7) b = %Blob{content: 123}
defmodule Blob do
  defstruct content: 123
end

Even though the question is solved, I do wonder why using defimpl Inspect, for: Any would work, given that an implementation for Map exists and it includes maps that contain:__struct__, keys.

Does my defimpl Inspect, for: Any jump to the top of the list?

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement