Search code examples
erlangcasen2o

Case statement issue in Erlang when working with n2o framework


Question about the case statement in Erlang.

I am using n2o as my web application framework.

In my sign in page i created an event to extract fields from the page when a user clicks the sign up button,

event(sign_up) - >

Gender = {Male,Female} = {wf:q(gm),wf:q(gf)},

Result = case Gender of 
    Gender when Male == true -> <<"M">>;
    Gender when Female == true -> <<"F">>;
    Gender when Male == false, Female == false -> <<"Not Selected">>
end,

error_logger:info_msg("Stuff",[{Result}]).

When I test the page, the logging code never gets hit. It only shows when i remove the case statement. Oddly, when executing the case statement in the shell, it evaluates correctly.

Am i missing something with my case statement?


Solution

  • After tracing, i did not realize that these functions within the tuple were returning string literals rather than atom populated patterns i was using for matching in the case statement.

    With that in mind the working solution:

    event(sign_up) - >
    
    Gender = {list_to_atom(wf:q(gm)),list_to_atom(wf:q(gf))},
    
    Result = case Gender of 
        {true,false} -> <<"M">>;
        {false,true} -> <<"F">>;
        Gender -> <<"Not Selected">>;
    end,
    

    So the lesson learned is make sure the data types you match are the same.

    It took a while :) but valuable lesson learned.