Elixir:允许重新绑定变量的基本原理 [英] Elixir: rationale behind allowing rebinding of variables

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

问题描述

在Erlang不允许的情况下,允许在Elixir中重新绑定变量的背后原理是什么?

What is the rationale behind allowing rebinding of variables in Elixir, when Erlang doesn't allow that?

推荐答案

大多数功能语言不允许在同一范围内重新绑定变量。因此,Elixir允许这样做确实给了它非功能性的,当务之急的感觉。 Erlang的问题是缺乏范围,或更确切地说,整个函数子句中只有一个范围。我们确实进行了认真的讨论,以决定是否引入范围,但最终我们决定反对范围,因为它与现有系统向后不兼容。开发人员讨厌向后不一致的更改。

Most functional languages don't allow rebinding of variables in the same scope. So Elixir allowing this does definitely give it an non-functional, imperative feel. Erlang's problem is rather lack of scope, or to be more precise that there is only one scope in a whole function clause. We did have serious discussions whether to introduce scope but in the end we decided against it as it was backwards incompatible with the existing system. And developers hate backwards inconsistent changes.

Erlang方式有一个严重的好处:错误时通常会收到错误,因此您可以查看 >错误。相比之下,当变量没有您期望的值很难更难检测和更正时,行为就会变得很奇怪。

The Erlang way has one serious benefit: when you get it wrong you generally get an error so you can SEE the mistake. This compared just getting strange behaviour when a variable doesn't have the value you expect it to have which is MUCH harder to detect and correct.

我个人认为,新变量名(例如使用数字方案)的问题被夸大了。与我计算做什么所花费的时间相比,更改变量名是微不足道的。过了一会儿,您只是看到它而没有思考它。坦白地说。

Personally I think that the problem of new variable names, for example using the number scheme, is greatly overblown. Compared to the time it takes me to work out WHAT I am going to do changing variable names is trivial. And after a while you just see it without reflecting about it. Honestly.

编辑:

当通过一系列函数链接数据时数据的实际含义发生了变化,因此重复使用相同的变量名可能会产生误导。它最终可能只是意味着一个通用的我正在从一个函数传递到另一个函数的数据。

Also when chaining data through a sequence of functions the actual meaning of the data changes so reusing the same variable name can be very misleading. It can end up just meaning a generic "data I am passing from one function to another".

这篇关于Elixir:允许重新绑定变量的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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