Erlang中的匿名变量 [英] Anonymous variables in Erlang

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

问题描述

下划线变量和以Erlang编译器的下划线开始的命名变量(除了增加可读性以外)的确切差异是什么?

What are the exact differences between underscore variables and a named variable that starts with underscore from the Erlang compiler point of view (apart from adding readability to the code)?

例如 _ _Var 不同?

推荐答案

不关心变量 _ 是一个 VERY SPECIAL 变量,匹配任何东西,并且永远绑定到一个值。当我知道有东西在那里时,它被使用,但我不在乎什么价值,我永远不会使用。看到 _ 从来没有绑定它不能在表达式中使用,编译器将其标记为错误。

The don't care variable _ is a VERY SPECIAL variable which matches anything and is NEVER bound to a value. It is used when I know there is something there but I don't care what the value is and I will never use. Seeing _ is never bound it can not be used in an expression and the compiler flags it as an error.

诸如 _Var 的变量是完全正常的变量,您可以匹配,并将绑定到值,这意味着它们可以在表达式中使用。使用 _ 预定义变量是关于意图。编译器通常会向您发出一个绑定在模式中但永远不会使用的变量,通常是错误的标志。但编译器不会以 _Var 中的变量前缀为 _ 发出警告。意图是我想给变量一个名字,命名的东西是好的,但是我知道我永远不会使用它。

Variables like _Var are perfectly normal variables which you can match against and will be bound to values which means they can be used in expressions. Prefixing a variable with _ is about intent. The compiler normally warns you about a variable which is bound in a pattern but is never used, often a sign of an error. But the compiler does not warn for variables prefixed with _ like in _Var. The intent being that I want to give the variable a name, naming things is good, but that I know I will never use it.

记住 _ 是唯一的特殊变量, _Var 是正常的变量,如果使用的话也是这样的。如果你感觉不正确,那么你可以使用 _ 的所有变量前缀,并且所有的变量仍然可以工作。

Remember that _ is really the only special variable and that _Var are normal variables and behave as such if used. If you are feeling perverse then you could prefix all your variables with _ and everything will still work.

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

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