在erlang中调用未定义的函数

在erlang中调用未定义的函数

本文介绍了在erlang中调用未定义的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将DATA fom表mnesia导出到txt文件

I want to export DATA fom table mnesia to the txt file

我尝试使用以下代码:

exporttxt()->
     F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
{atomic,L} = mnesia:transaction(F(user)),
file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
                 #user{id = F1,adress = F2,birthday = F3} <- L]).

但是当我测试这个函数时,我有这个错误:

but when I test this function I have this error :

Erlang R13B03 (erts-5.7.4) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1> model:exporttxt().
** exception error: undefined function mensia:foldl/3
     in function  model:exporttxt/0
2>

如您所见,我使用erlang版本13

as you see I work with erlang version 13

我现在尝试使用以下代码:

exporttxt()->
     F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
{atomic,L} = mnesia:transaction(F(user)),
file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
                 #user{id = F1,adress = F2,birthday = F3} <- L]).

但我有这个错误:

** exception exit: {aborted,no_transaction}
     in function  mnesia:abort/1
     in call from model:exporttxt/0

我也尝试:

 exporttxt()->
         F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
    {atomic,L} = mnesia:transaction(F),
    file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
                     #user{id = F1,adress = F2,birthday = F3} <- L]).

但我有这个错误:

** exception error: no match of right hand side value
                    {aborted,{{badarity,{#Fun<model.208.16694406>,[]}},
                              [{mnesia_tm,apply_fun,3},
                               {mnesia_tm,execute_transaction,5},
                               {model,exporttxt,0},
                               {erl_eval,do_apply,5},
                               {shell,exprs,6},
                               {shell,eval_exprs,6},
                               {shell,eval_loop,3}]}}
     in function  model:exporttxt/0


推荐答案

mnesia:transaction(F(user))

这是错误的,花一点时间看看。 mnesia:transaction / 1期望一个函数,你没有传递一个函数,你通过F(user)的结果是..(记住erlang使用严格的评估,所以它评估F用户) mnesia:transaction / 1)

that is wrong, take a minute and look at it. mnesia:transaction/1 expect a function, you aren't passing it a function, you pass whatever the result of F(user) is.. (remember erlang uses strict evaluation, so it evaluates F(user) before mnesia:transaction/1).

在你的情况下,对F(用户)的调用失败,因为它预计会在里面运行一个交易。

In your case that call to F(user) fails because it was expected to run inside a transaction.

这篇关于在erlang中调用未定义的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:22