lc0815

lc0815

String.char error - (Protocol.UndefinedError) protocol String.Chars not implemented

this code is from a course from pragmaticstudio

def parse(request) do
#TODO: parse the request string into a map:
first_line = request |> String.split(“\n”) |> List.first
[method, path, _] = String.split(first_line, " ")
%{ method: method, path: path, resp_body: “”}
end

this line:
%{ method: method, path: path, resp_body: “”}
gives me the error:

== Compilation error in file lib/servy/handler.ex ==
** (Protocol.UndefinedError) protocol String.Chars not implemented for %{method: "GET", path: "/wildthings", resp_body: ""} of type Map
    (elixir 1.12.3) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir 1.12.3) lib/string/chars.ex:22: String.Chars.to_string/1
    (elixir 1.12.3) lib/io.ex:712: IO.puts/2
    (elixir 1.12.3) lib/kernel/parallel_compiler.ex:319: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

but the code does not give an error on their screen.

the full error message:

(Protocol.UndefinedError) protocol String.Chars not implemented for %{method: "GET", path: "/wildthings", resp_body: ""} of type Map

Most Liked

al2o3cr

al2o3cr

IO.inspect returns its argument so your route function passes the map along to… whatever code is actually calling IO.puts (still not shown :frowning: ) and you get the same error.

Commenting out the whole IO.inspect line will make route return nil, which IO.puts is fine with (it prints a single newline).

al2o3cr

al2o3cr

The error appears to be caused by passing the result of parse to IO.puts; can you show that code?

FWIW, IO.inspect is usually a better tool for printing things mid-calculation, as it can show maps/tuples/PIDs/etc that don’t work with IO.puts.

lc0815

lc0815

thanks for all of your comments, the issue were some hidden chars on the file, I created a new file and re-typed the code and the problem went away… need a new keyboard LoL, got to blame it on something…
anywho: alls ok now…

Where Next?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

Other popular topics Top

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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement