gpt4 book ai didi

erlang - 在Erlang shell中,如何自动读取所有记录定义 header

转载 作者:行者123 更新时间:2023-12-03 02:43:00 26 4
gpt4 key购买 nike

这从问题开始:

几乎每次使用 Erlang shell 时,我都想在 shell 启动时运行一些命令,例如类似的东西

rr("*.hrl").

或类似的。目前我每次启动 Erlang shell 时都必须输入它,而且我已经厌倦了并且总是忘记它。

但是这实际上是一个错误的问题!因为我真正想做的是在每个 shell 作业中读取我的记录定义 header 。不用于在启动时运行的其他 shell 内置命令。因此,我更改了问题标题以显示应该如何提出的问题。

最佳答案

在尝试使用 .erlang 解决方案时,我偶然发现了针对特定 rr/1 用法的解决方案:

来自 shell 的手册页:

There is some support for reading and printing records in the shell.During compilation record expressions are translated to tuple expres-sions. In runtime it is not known whether a tuple actually represents arecord. Nor are the record definitions used by compiler available atruntime. So in order to read the record syntax and print tuples asrecords when possible, record definitions have to be maintained by theshell itself. The shell commands for reading, defining, forgetting,listing, and printing records are described below. Note that each jobhas its own set of record definitions. To facilitate matters recorddefinitions in the modules shell_default and user_default (if loaded)are read each time a new job is started. For instance, adding the line

  -include_lib("kernel/include/file.hrl").

to user_default makes the definition of file_info readily available inthe shell.

为了清楚起见,我添加一些示例:

文件foo.hrl:

-record(foo, {bar, baz=5}).

文件:user_default.erl:

-module(user_default).
-compile(export_all).

-include("foo.hrl"). % include all relevant record definition headers here

%% more stuff probably ...

让我们在 shell 中尝试一下:

$ erl
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5 (abort with ^G)
1> #foo{}.
#foo{bar = undefined,baz = 5}

→ shell 知道来自 foo.hrl

的记录

关于erlang - 在Erlang shell中,如何自动读取所有记录定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4071788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com