uwiger / parse_trans

Parse transform utilities for Erlang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

codegen:gen_module/3 failing unexpectedly

tolbrino opened this issue · comments

I tried the gen_module API which has been introduced just recently. Maybe I'm using it incorrectly, but all other APIs work fine so I'm at a loss. However, I didn't look into it deeply yet.

Funs = [
        {render, fun() -> x end},
        {source, fun() -> ok end}
    ],
    codegen:gen_module(test, [{render,0}, {source, 0}], Funs).

Fails with:

priv/gen-api-yang-to-erl.escript:34: exception error: no case clause matching variable
  in function  erl_syntax:list_elements/2 (erl_syntax.erl, line 2438)
  in call from erl_syntax:list_elements/1 (erl_syntax.erl, line 2435)
  in call from parse_trans_codegen:gen_module_/5 (src/parse_trans_codegen.erl, line 234)
escript: There were compilation errors.

Since codegen:gen_module/3 is not a real function call, some restrictions apply. The third argument needs to be an explicit list:

    codegen:gen_module(test, [{render,0}, {source, 0}],
               [
            {render, fun() ->
                     x end},
            {source, fun() ->
                     ok end}
               ]).

(Well, "needs to be" - it's coded that way, but it would be possible to dig up a variable reference, at least if it's straightforwardly bound in the same scope. Not something I will do right now, though.)

I have pushed an update that cleans up the error a little bit. Getting a crash in erl_syntax is not exactly user-friendly...

Ah, that makes sense. No need to add support for a variable reference. I went for straight erl_syntax since I needed some more control anyway. But gen_module does come in very handy. Thanks!