我有以下类型:
trait Monster {
fn attack(&self);
fn new(int) -> Self;
}
struct CookiesMonster {
cookies: int,
hungry_level: int,
}
impl Monster for CookiesMonster {
fn new(i: int) -> CookiesMonster {
CookiesMonster { cookies: i, hungry_level: i + 1 }
}
fn attack(&self) {
println!("I have {:d} cookies!!", self.cookies)
}
}
struct Dummy {
count: int
}
impl Dummy {
fn new(i: int) -> Dummy {
Dummy { count: i }
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这有效:
let monster: CookiesMonster = Monster::new(10);
let dummy …Run Code Online (Sandbox Code Playgroud) 以下代码应该读取文件并Item为每行创建记录:
defmodule Ship do
defrecord Item, product_code: 0, quantity: 0, destination: ""
def load_data do
File.read!("data")
|> String.split
|> Enum.map &(String.split &1, ",")
|> Enum.map &(list_to_item &1)
end
defp list_to_item([pc, q, d | []]) do
{parsed_q, _} = Integer.parse q
Item.new product_code: pc, quantity: parsed_q, destination: d
end
end
Run Code Online (Sandbox Code Playgroud)
有一个data包含以下内容的文件:
1,100,London
1,30,Lisbon
3,2,Braga
Run Code Online (Sandbox Code Playgroud)
问题是,当我执行load_data函数时,看起来最后一次调用Enum.map试图递归调用父函数(load_data)并抛出一个BadArityError.
输出如下:
iex(1)> Ship.load_data
** (BadArityError) #Function<0.127338698/1 in Ship.load_data/0> with arity 1 called with 2 arguments ({:cont, []}, #Function<30.103209896/2 in Enum.map/2>) …Run Code Online (Sandbox Code Playgroud)