2015-05-28 65 views

回答

10

你要么需要匹配字段中手动

defmodule Test do 
    def foo(%User{name: name, twitter: twitter}, {User, name, twitter}) do 
    IO.puts "match :)" 
    end 

    def foo(_struct, _record) do 
    IO.puts "no match :(" 
    end 
end 

,或者你需要将它转化成一个结构,然后再匹配两个

defmodule Test do 
    def foo(struct, record) do 
    do_foo struct, user_record_to_struct(record) 
    end 

    defp user_record_to_struct({User, name, twitter}) do 
    %User{name: name, twitter: twitter} 
    end 

    defp do_foo(struct, struct) do 
    IO.puts "match :)" 
    end 

    defp do_foo(_struct1, _struct2) do 
    IO.puts "no match :(" 
    end 
end 
+4

既然你开始“据我所知” ,我只想确认这个答案是完全正确的。 :) –

+0

谢谢,@JoséValim删除它;-) –