erlang mnesia - 非法记录信息 [英] erlang mnesia - illegal record info

查看:174
本文介绍了erlang mnesia - 非法记录信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拥有一个功能,确保我需要的表已经被创建,如果不是创建它。以下是示例:

  ensure_table_exists(表,MnesiaTables,节点) - > 
案例列表:
的成员(表,MnesiaTables)true - >
throw({error,db_might_have_already_been_created});
false - >
mnesia:create_table(Table,[{disc_copies,Nodes},
{attributes,record_info(fields,Table)}]),
ok
end。

问题是编译时收到错误:非法记录信息
它可能需要这样做在编译时解析record_info或记录信息的第二个参数实际上应该是可以从源代码检索的记录?

解决方案

是的,包括 record_info / 2 在内的所有相关记录在编译时解决。这意味着在编译时必须知道记录和字段名称。这是编译器错误的原因。



我不认为你的功能真的太防御了,因为你正在做的是发出更具体的错误。如果您要返回 {error,...}



最后一点是,如果你提出一个例外,你不应该使用 throw / 1 而是使用 erlang:error / 1 throw 旨在用于非本地返回(捕获 catch ),而 erlang:error 旨在提高异常。在许多情况下,结果可能相同,但实际的错误值可能会误导( nocatch )。总是更好的是你可以更清楚地显示出你的意图,在这种情况下,这表示错误。



是的,我知道 catch 也捕获错误/退出。这是故意的。在一个完美的世界中,可能 catch 应该只能捕捉和尝试只有错误/退出。


I am trying to have a function that ensures the table I need is already created and if not to create it. Here's the sample:

ensure_table_exists(Table, MnesiaTables, Nodes) ->
case lists:member(Table, MnesiaTables) of
    true ->
        throw({error, db_might_have_already_been_created});
    false ->
        mnesia:create_table(Table, [{disc_copies, Nodes},
                {attributes, record_info(fields, Table)}]), 
        ok  
end.

The issue is that when compiling I get the error: illegal record info. It might have to do that record_info is resolved at compile time or that the second argument to record info should actually be a record that can be retrieved from the source code ?

解决方案

Yes, all record related things including record_info/2 are resolved at compile time. This means that record and field names must be known at compile time. This is the reason for the compiler error.

I don't think your function is really too defensive in that what you are doing is signaling a more specific error. It would be another matter if you were to return {error, ...}.

One last point is that if you mean to raise an exception you should not use throw/1 but instead use erlang:error/1. throw is intended for non-local return (caught with a catch) while erlang:error is intended for raising an exception. In many cases the result may be the same but the actual error value may be misleading (nocatch). It is always better the clearer you can show your intention, which in this case is signaling an error.

P.S. Yes, I know that catch also catches errors/exits as well. This was intentional. In a perfect world maybe catch should only catch throws and try only errors/exits.

这篇关于erlang mnesia - 非法记录信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆