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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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 Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
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 27727 240
New
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement