Pattern matching - implementation

后端 未结 4 670
执念已碎
执念已碎 2020-12-13 09:14

I am wondering how pattern matching is usually implemented. for example in Erlang do you think its implemented at the byte-code level( there\'s a byte-code for it so that it

4条回答
  •  旧巷少年郎
    2020-12-13 10:00

    The best thing I can suggest is to compile up some test functions and have a look at the generated code.

    erlc -S test.erl
    

    generates test.S which is fairly readable.

    To answer the question, pattern matches are built up in an efficient way from more primitive operations. Here's part of the code from a function clause matching {X, [H|T]}.

    {test,is_tuple,{f,1},[{x,0}]}.
    {test,test_arity,{f,1},[{x,0},2]}.
    {get_tuple_element,{x,0},0,{x,1}}.
    {get_tuple_element,{x,0},1,{x,2}}.
    {test,is_nonempty_list,{f,4},[{x,2}]}.
    

提交回复
热议问题