maps string question

Java House java4dev@REDACTED
Wed Oct 13 23:05:33 CEST 2021


Hello
I am having a problem with maps and strings as keys.
My understanding that the the two string formats "string" and <<"string">>
are identical but as you can see from the transcript bellow maps does not
think so.
I searched but I couldn't find any function to convert form "string" to
<<"string">> or the other way around.
How can I make maps to accept both format of strings?

Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10]
[async-threads:1] [jit]

Eshell V12.0  (abort with ^G)
1> Key1 = "key1".
"key1"
2>
2> Key11 = <<"key1">>.
<<"key1">>
3>
3> string:equal(Key1, Key11).
true
4>
4> Map = maps:new().
#{}
5>
5> Map1 = maps:put("key1", {something}, Map).
#{"key1" => {something}}
6>
6> maps:get(Key1, Map1).
{something}
7>
7> maps:get(Key11, Map1).
** exception error: bad key: <<"key1">>
     in function  maps:get/2
        called as maps:get(<<"key1">>,#{"key1" => {something}})
        *** argument 2: not a map
8>
8> maps:get("key1", Map1).
{something}
9>
9> maps:get(<<"key1">>, Map1).
** exception error: bad key: <<"key1">>
     in function  maps:get/2
        called as maps:get(<<"key1">>,#{"key1" => {something}})
        *** argument 2: not a map
10>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20211013/ce916108/attachment.htm>


More information about the erlang-questions mailing list