变量在Erlang [英] Variable in Erlang

查看:136
本文介绍了变量在Erlang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的Erlang程序:

I have a very simple Erlang program:

-module(test).
-export([start/0]).

Code = "Z00887". 
start() -> io:fwrite(Code).

我有两个错误:


c:/erl6.1/dev/test.erl:4:之前的语法错误:代码

c:/erl6.1/dev/test.erl:5:variable 代码未绑定

c:/erl6.1/dev/test.erl:4: syntax error before: Code
c:/erl6.1/dev/test.erl:5: variable 'Code' is unbound

请问如何正确使用代码中的变量。

Could you please help me correctly using variables in my code.

推荐答案

您正在定义一个对模块是全局的变量,这是不允许的。记住,Erlang中的变量是真正的符号,所以在所有函数或过程中都不存在全局常量的概念。在Erlang中最接近的事情是在模块中定义的一个宏,但是如果一个值只需要在一个地方,并且你想要命名,那么这必须在函数定义中完成。

You are defining a variable that is global to the module, which is not allowed. Remember, "variables" in Erlang are really "symbols", so there is no concept of a "global" constant across all functions or processes. The closest thing to this in Erlang would be a macro defined in the module, but if a value is only needed in one place and you want to name it then this must be done within the function definition.

另外,不要使用io:fwrite / 1或io:format / 1。问题是可能在你传递的字符串中包含转义字符。例如,这会导致错误: Code =Wee〜!,io:format(Code)。并且不会被编译器捕获。

Also, do not use io:fwrite/1 or io:format/1. The problem is the possible inclusion of escape characters in the string you are passing. For example, this causes an error: Code = "Wee~!", io:format(Code). and it will not be caught by the compiler.

最常见的事情是在函数中定义一个变量:

The most common thing to do is define a variable within the function:

-module(foo).
-export([start/0]).

start() ->
    Code = "Z00887",
    io:fwrite("~p~n", [Code]).

您也可以直接使用该值:

You could also just use the value directly:

-module(foo).
-export([start/0]).

start() ->
    io:fwrite("Z00887~n").

或者您可以在整个模块中定义一个宏:

Or you could define a macro across the whole module:

-module(foo).
-export([start/0]).

-define(CODE, "Z00887").

start() ->
    io:fwrite("~p~n", [?CODE]).

或者你甚至可以定义一个返回你想要的存根功能:

Or you could even define a stub function that returns what you want:

-module(foo).
-export([start/0]).

start() ->
    io:fwrite("~p~n", [code()]).

code() -> "Z00887".

这最后一个版本实际上是不是/ 。很多时候,早期开发一些代码时,你会知道你需要一定的价值,你需要以某种方式派生,但不想担心它的细节。一个存根功能是一个很好的方法来隐藏你将来会怎么做的细节,而不需要编写宏代码,变量定义等,你将不得不记得回去改变。例如,上面的最后一个例子将来肯定会变成这样的东西:

This last version is actually not as weird as it may seem at first. Very often when developing some code early on you will know you need a value somewhere that you will need to derive in some way, but don't want to worry about the details of it just yet. A stub function is an excellent way to hide the details of how you will do that in the future without writing macro code, variable definitions, etc. that you will have to remember to go back and change later. For example, the last example above will almost certainly change to something like this in the future:

-module(foo).
-export([start/0]).

start() ->
    io:fwrite("~p~n", [code()]).

code() ->
    {ok, Code} = some_init_module:get_code(),
    Code.

记住这一点。它使Erlang几乎像Guile或Scheme一样对原型友好。

Keep this in mind. It makes Erlang almost as prototype-friendly as Guile or Scheme.

这篇关于变量在Erlang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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