Venkatesh Shanmugham
Venkatesh Shanmugham

Reputation: 81

Elixir : Issue in defining Maps

I have a predefined Maps with a number as key and its value, I tried by creating maps like below this in CL it's working fine but when I execute through code file it throws an error like.

I got an error saying

** (Protocol.UndefinedError) protocol String.Chars not implemented for %{1 => "I", 4 => "IV", 5 => "V", 9 => "VI", 10 => "X", 40 => "XL", 50 => "L", 90 => "XC", 100 => "C", 400 => "CD", 500 => "D", 900 => "CM", 1000 => "M"} of type Map
    (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir) lib/string/chars.ex:22: String.Chars.to_string/1
    (elixir) lib/io.ex:654: IO.puts/2

When I try to define a pre-defined key-value.

defmodule DecimalToRoman do
  def convert(decimal) do
    roman_table = %{
      1 => "I",
      4 => "IV",
      5 => "V",
      9 => "VI",
      10 => "X",
      40 => "XL",
      50 => "L",
      90 => "XC",
      100 => "C",
      400 => "CD",
      500 => "D",
      900 => "CM",
      1000 => "M"
    }

    IO.puts(roman_table)
  end
end

DecimalToRoman.convert(2012)

I want to use number as Key.

Upvotes: 2

Views: 217

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 5995

Use IO.inspect(roman_table).

IO.puts requires the argument to be either a string, or anything that implements the String.Chars protocol while IO.inspect can print an arbitrary elixir term.

The core library implements String.Chars for Atom, BitString, List, Integer, and Float.

Upvotes: 6

Related Questions