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

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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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