【发布时间】:2020-06-29 01:03:20
【问题描述】:
我有一个函数,它接收字符串列表并将每个字符串连接成一个新字符串,我使用 Enum.join 执行此操作。但是当我尝试这个操作时,我得到了以下错误:
** (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
我的解决方法是尝试将 BitString 转换为字符串,但我在 Elixir 的文档中找不到任何这样做的东西。 我的另一个解决方案是试图根本不获取那个 BitString,但我什至不知道为什么我要从那个 BitString 开始。
我正在做的过程是接收这样的列表:[{"int main(){return 2;}", 1}]
然后我列出一个列表,但只使用字符串text=Enum.map(words, fn {string, _} -> string end)
我尝试打印结果,所以我确定我给出了正确的论点;通过使用IO.inspect(text),我得到了["int main(){return 2;}"],这对我来说就像一个字符串列表。
然后我将它传递给使用Enum.flat_map(text, &lex_raw_tokens(&1, line))的函数
在那个函数里面,我做
def lex_raw_tokens(program,line) when program != "" do
textString=Enum.join(program, " ")
这是我得到错误的地方。有什么办法可以将那个 BitString 转回一个 String 或者不得到那个 BitString?
抱歉,我还在学习 Elixir,老实说,到目前为止,这是我学过的最难的语言,而且我遇到了很多麻烦。此外,这整件事是我作为学校项目所做的一个小型 C 编译器的一部分
【问题讨论】:
-
很遗憾,不,这是我自己的问题,我承认我不够具体,所以没有人能回答。
-
标题极具误导性,所以我把它作为上一个的副本关闭了。
标签: string types erlang elixir bitstring