Giezy Ramírez
Giezy Ramírez

Reputation: 91

How can i convert a BitString into a String in Elixir?

I have a function that receives a list of strings and concatenates each string into a new string, i do that using Enum.join. But when i try this operation, i get the following error:

** (Protocol.UndefinedError) protocol Enumerable not implemented for "int main(){return 2;}" of type BitString. This protocol is implemented for the following type(s): Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream

My way around this was trying to convert the BitString into a String, but i can't find anything for doing this in Elixir's documentation. My other solution was trying to not get that BitString at all but i don't even know why i'm getting that BitString to begin with.

The process i'm doing is to receive a list like this: [{"int main(){return 2;}", 1}] Then i make a list but only using the string text=Enum.map(words, fn {string, _} -> string end)

I tried printing the result so i'm sure i'm giving the correct argument; by using IO.inspect(text), i got ["int main(){return 2;}"], which looks like a list of strings to me.

Then i pass that to a function using Enum.flat_map(text, &lex_raw_tokens(&1, line))

Inside of that function, i do def lex_raw_tokens(program,line) when program != "" do textString=Enum.join(program, " ")

This is where i get the error. Is there any way of turning that BitString back into a String or not get that BitString?

Sorry, i'm still learning Elixir and honestly so far it's the most dificcult lenguage i've learned and i'm having a lot of troubles with it. Also, this whole thing is part of a small C compiler i'm doing as a school proyect

Upvotes: 0

Views: 1447

Answers (1)

Brett Beatty
Brett Beatty

Reputation: 5963

You have text bound to ["int main(){return 2;}"], then you're doing an Enum.flat_map/2 over text, so inside lex_raw_tokens/2, program is bound to "int main(){return 2;}". You're then trying to do an Enum.join/2 on program, but since it's a string (which is a kind of BitString), it's not enumerable.

Upvotes: 1

Related Questions