[foo, bar] = [“foo”, “bar”] 功能的名称是什么?
我需要知道某些语言提供的这个很酷的功能的正确名称。
仅供参考:在某些语言中,可以通过将值结构分配给“变量”结构来进行多重分配。在问题标题的示例中,它将“foo”分配给 foo,将“bar”分配给 bar。
I need to know a correct name for this cool feature that some languages provide.
FYI: In some languages it is possible to do a multiple assignments by assigning a structure of values to a structure of "variables". In the example in the question title it assigns "foo" to foo and "bar" to bar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它通常在函数式语言(没有赋值)中称为“解构绑定”,在命令式语言中称为“解构赋值”。
有些语言提供该功能的子集,然后将其称为不同的名称。例如,在 Python 中,它适用于元组、列表或序列,称为“元组解包”、“列表解包”或“序列解包”,在 Ruby 中,它称为“元组解包”、“列表解包”或“序列解包”。与数组(或可转换为数组的对象)一起使用,称为并行赋值。
解构绑定可以变得任意复杂。例如,此(虚构的)绑定
会将
some_array
的第一个元素分配给a
,将第二个元素分配给b
,将第四个元素分配给c
,但仅如果第一个元素是Integer
,第三个元素等于2
且长度为4。因此,这甚至包含了一些条件逻辑。解构绑定是更通用的模式匹配的子集,它是 Haskell、ML、OCaml、F#、Erlang 和 Scala 等函数式语言的标准功能。不同之处在于,解构绑定仅允许您分解结构并将其组件绑定到变量,而模式匹配还匹配这些结构内的值并允许您做出决策,特别是允许您在绑定的上下文中运行任意代码。 (您可以将上面的假想绑定视为解构绑定和模式匹配之间的中间位置。)
以下是假想语言中的
reverse
函数的经典示例,使用模式匹配编写:It's generally called destructuring bind in functional languages (which don't have assignments) and destructuring assignment in imperative languages.
Some languages provide subsets of that feature and then call it something different. For example, in Python it works with Tuples, Lists or Sequences and is called Tuple unpacking, List unpacking or Sequence unpacking, in Ruby, it works with Arrays (or objects that are convertible to an array) and is called parallel assignment.
Destructuring bind can get arbitrarily complex. E.g. this (imaginary) bind
would assign the first element of
some_array
toa
, the second element tob
and the fourth element toc
, but only if the first element is anInteger
, the third element is equal to2
and the length is 4. So, this even incorporates some conditional logic.Destructuring bind is a subset of more general pattern matching, which is a standard feature of functional languages like Haskell, ML, OCaml, F#, Erlang and Scala. The difference is that destructuring bind only lets you take apart a structure and bind its components to variables, whereas pattern matching also matches on values inside those structures and lets you make decisions and in particular lets you run arbitrary code in the context of the bindings. (You can see the above imaginary bind as half-way in between destructuring bind and pattern matching.)
Here's the classical example of a
reverse
function in an imaginary language, written using pattern matching:在 Python 中,它被称为列表或序列解包:http://docs. python.org/tutorial/datastructs.html#tuples-and-sequences
In Python it is known as list or sequence unpacking: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
在 Ruby 和其他语言中这称为并行赋值。
It's called parallel assignment in Ruby and other languages.
Perl 和 PHP 称之为列表赋值
Perl:
PHP:
Perl and PHP call it list assignment
Perl:
PHP:
如果您将右侧视为元组,则可以将分配视为一种 元组拆包。
If you view the right hand side as a tuple, one could view the assignment as a kind of Tuple Unpacking.
在 Erlang 中,它......好吧,这不是赋值,而是模式匹配(因为在 Erlang 中没有赋值)。
为什么叫“模式匹配”?因为它实际上是匹配模式。看:
在第一个中,我们所做的实际上相当于一个断言,即列表将以
[1,2,3,4]
开头,并且第五个值可以是任何值,但请绑定它进入未绑定变量A
。在第二个中,我们做了同样的事情,只是A
现在已绑定,因此我们显式查找列表[1,2,3,4,5]
(因为A
现在是5
)。In Erlang it's ... well, it's not assignment, it's pattern matching (seeing as there is no assignment, as such, in Erlang).
Why is it called "pattern matching"? Because it actually is matching patterns. Look:
In the first one we did what effectively amounts to an assertion that the list would start with
[1,2,3,4]
and that the fifth value could be anything at all, but please bind it into the unbound variableA
. In the second one we did the same thing except thatA
is now bound so we're looking explicitly for the list[1,2,3,4,5]
(becauseA
is now5
).Mozilla 将其称为解构赋值。在Python中,它是序列解包;元组拆包是一种常见的特殊情况。
Mozilla calls it destructuring assignment. In Python, it's sequence unpacking; tuple unpacking is a common special case.
在 Clojure 中,它被称为“解构”。简单的例子:
它也经常用于函数定义中,例如以下将单点参数解构为 x 和 y 分量:
您还可以使用相同的技术来解构嵌套数据结构或键/值关联映射。
In Clojure it would be called destructuring. Simple example:
It's also often used in function definitions, e.g. the following destructures a single point argument into x and y components:
You can also use the same technique to destructure nested data structures or key/value associative maps.