2015-05-28 8 views
8

पैटर्न के खिलाफ एक रिकॉर्ड के साथ पैटर्न मिलान करने का कोई तरीका है? E.g ने एक रिकॉर्ड और नीचे एक संरचना दी।पैटर्न मिलान एलिक्सीर रिकॉर्ड आंगनवादी संरचना

struct = %User{name: "", twitter:""} 
record = {User, "mossplix ", "@mossplix"} 

उत्तर

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 

मैच के लिए की जरूरत है या आप इसे एक struct पहले में बदलने और फिर दो

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

से मेल खाते हैं जब से तुम साथ "AFAIK" शुरू कर दिया की जरूरत है , मैं सिर्फ यह पुष्टि करना चाहता हूं कि यह उत्तर बिल्कुल सही है। :) –

+0

धन्यवाद, @ जोसेवलिम ने इसे हटा दिया ;-) –

संबंधित मुद्दे