Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Assignment (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Parallel assignment=== Some programming languages, such as [[APL (programming language)|APL]], [[Common Lisp]],<ref>{{cite web |title=CLHS: Macro SETF, PSETF |url=http://www.lispworks.com/documentation/HyperSpec/Body/m_setf_.htm#psetf |website=Common Lisp Hyperspec |publisher=LispWorks |access-date=23 April 2019}}</ref> [[Go (programming language)|Go]],<ref>The Go Programming Language Specification: [http://golang.org/ref/spec#Assignments Assignments]</ref> [[JavaScript]] (since 1.7), [[Julia (programming language)|Julia]], [[PHP]], [[Maple (software)|Maple]], [[Lua (programming language)|Lua]], [[occam (programming language)#occam 2|occam 2]],<ref name="occam">{{cite book |editor=INMOS Limited |year=1988 |title=Occam 2 Reference Manual |location=New Jersey |publisher=Prentice Hall |isbn=0-13-629312-3}}</ref> [[Perl]],<ref name="perl">{{cite book |last=Wall |first=Larry |author-link=Larry Wall |author2=Christiansen, Tom |author3=Schwartz, Randal C. |year=1996 |edition=2 |title=Perl Programming Language |location=Cambridge |publisher=O´Reilly |isbn=1-56592-149-6 |url=https://archive.org/details/programmingperl00wall }}</ref> [[Python (programming language)|Python]],<ref name="python">{{cite book |last=Lutz |first=Mark |year=2001 |edition=2 |title=Python Programming Language |location=Sebastopol |publisher=O´Reilly |isbn=0-596-00085-5 |url=https://archive.org/details/programmingpytho00lutz }}</ref> [[REBOL]], [[Ruby (programming language)|Ruby]],<ref name="ruby">{{cite book |last=Thomas |first=David |author2=Hunt, Andrew |year=2001 |title=Programming Ruby: The Pragmatic Programmer's Guide |location=Upper Saddle River |publisher=Addison Wesley |isbn=0-201-71089-7 |url=https://archive.org/details/programmingruby000thom }}</ref> and [[PowerShell]] allow several variables to be assigned in parallel, with syntax like: a, b := 0, 1 which simultaneously assigns 0 to <code>a</code> and 1 to <code>b</code>. This is most often known as '''parallel assignment'''; it was introduced in [[CPL (programming language)|CPL]] in 1963, under the name '''simultaneous assignment''',<ref>D.W. Barron ''et al.'', "The main features of CPL", ''Computer Journal'' '''6''':2:140 (1963). [https://archive.today/20120707200431/http://comjnl.oxfordjournals.org/cgi/reprint/6/2/134 full text (subscription)]</ref> and is sometimes called '''multiple assignment''', though this is confusing when used with "single assignment", as these are not opposites. If the right-hand side of the assignment is a single variable (e.g. an array or structure), the feature is called '''unpacking'''<ref>{{cite web|url=http://legacy.python.org/dev/peps/pep-3132/|title=PEP 3132 -- Extended Iterable Unpacking|website=legacy.python.org|access-date=20 April 2018}}</ref> or '''destructuring assignment''':<ref>{{cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment|title=Destructuring assignment|website=MDN Web Docs|access-date=20 April 2018}}</ref> '''var''' list := {0, 1} a, b := list The list will be unpacked so that 0 is assigned to <code>a</code> and 1 to <code>b</code>. Furthermore, a, b := b, a swaps the values of <code>a</code> and <code>b</code>. In languages without parallel assignment, this would have to be written to use a temporary variable '''var''' t := a a := b b := t since <code>a := b; b := a</code> leaves both <code>a</code> and <code>b</code> with the original value of <code>b</code>. Some languages, such as [[Go (programming language)|Go]], [[F Sharp (programming language)|F#]] and [[Python (programming language)|Python]], combine parallel assignment, tuples, and automatic [[b:Python Programming/Tuples#Packing and Unpacking|tuple unpacking]] to allow multiple return values from a single function, as in this Python example, <syntaxhighlight lang="python"> def f(): return 1, 2 a, b = f() </syntaxhighlight> while other languages, such as [[C sharp (programming language)|C#]] and [[Rust (programming language)|Rust]], shown here, require explicit tuple construction and deconstruction with parentheses: <syntaxhighlight lang="csharp"> // Valid C# or Rust syntax (a, b) = (b, a); </syntaxhighlight> <syntaxhighlight lang="csharp"> // C# tuple return (string, int) f() => ("foo", 1); var (a, b) = f(); </syntaxhighlight> <syntaxhighlight lang="rust"> // Rust tuple return let f = || ("foo", 1); let (a, b) = f(); </syntaxhighlight> This provides an alternative to the use of [[output parameter]]s for returning multiple values from a function. This dates to [[CLU (programming language)|CLU]] (1974), and CLU helped popularize parallel assignment generally. C# additionally allows generalized ''deconstruction assignment'' with implementation defined by the expression on the right-hand side, as the compiler searches for an appropriate [[instance method|instance]] or [[extension method|extension]] <code>Deconstruct</code> method on the expression, which must have output parameters for the variables being assigned to.<ref>{{cite web |title=Deconstructing tuples and other types |url=https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct |website=Microsoft Docs |publisher=Microsoft |access-date=29 August 2019}}</ref> For example, one such method that would give the [[class (computer programming)|class]] it appears in the same behavior as the return value of <code>f()</code> above would be <syntaxhighlight lang="csharp"> void Deconstruct(out string a, out int b) { a = "foo"; b = 1; } </syntaxhighlight> In C and C++, the [[comma operator]] is similar to parallel assignment in allowing multiple assignments to occur within a single statement, writing <code>a = 1, b = 2</code> instead of <code>a, b = 1, 2</code>. This is primarily used in [[Comma operator#For loops|for loops]], and is replaced by parallel assignment in other languages such as Go.<ref>[http://golang.org/doc/effective_go.html Effective Go]: [http://golang.org/doc/effective_go.html#for for], "Finally, Go has no comma operator and ++ and -- are statements not expressions. Thus if you want to run multiple variables in a for you should use parallel assignment (although that precludes ++ and --)."</ref> However, the above C++ code does not ensure perfect simultaneity, since the right side of the following code <code>a = b, b = a+1</code> is evaluated after the left side. In languages such as Python, <code>a, b = b, a+1</code> will assign the two variables concurrently, using the initial value of a to compute the new b.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)