What does ":=" do?
Asked Answered
T

12

130

I've seen := used in several code samples, but never with an accompanying explanation. It's not exactly possible to google its use without knowing the proper name for it.

What does it do?

Taxis answered 17/3, 2011 at 20:15 Comment(7)
In what language?Mete
You normally use := when you define something, to separate it from regular variable changes.. What programming language are we talking about?Ondrea
PL/SQL it is for assignment. But given a different language, that answer isn't guarenteed to hold true - so which languages was the example in?Shin
To google something like this, spell it out and enclose it in quotes, like so: "colon equals"Traditional
I think Pascal's got this operator !Shark
You can search for special symbols using this service.Bertold
The OP might have been referring to pseudocode, in which I've often seen :=.Aseity
C
119

http://en.wikipedia.org/wiki/Equals_sign#In_computer_programming

In computer programming languages, the equals sign typically denotes either a boolean operator to test equality of values (e.g. as in Pascal or Eiffel), which is consistent with the symbol's usage in mathematics, or an assignment operator (e.g. as in C-like languages). Languages making the former choice often use a colon-equals (:=) or ≔ to denote their assignment operator. Languages making the latter choice often use a double equals sign (==) to denote their boolean equality operator.

Note: I found this by searching for colon equals operator

Cq answered 17/3, 2011 at 20:19 Comment(4)
Ironically, this answer is now above Wikipedia when searching for colon equals operator.Inadvertency
If we keep typing colon equals operator, we work magic on Google's SEO to make this the top resultNeumark
That link looks old. Here's the updated link (I think), but the quote seems to be massively changed since then and I can't track down the new quote exactly: en.wikipedia.org/wiki/Assignment_(computer_science).Machutte
Smalltalk was also an influential language that used the colon equals operator for assignment.Ventris
E
54

It's the assignment operator in Pascal and is often used in proofs and pseudo-code. It's the same thing as = in C-dialect languages.

Historically, computer science papers used = for equality comparisons and for assignments. Pascal used := to stand in for the hard-to-type left arrow. C went a different direction and instead decided on the = and == operators.

Environs answered 17/3, 2011 at 20:20 Comment(0)
H
29

In the statically typed language Go := is initialization and assignment in one step. It is done to allow for interpreted-like creation of variables in a compiled language.

// Creates and assigns
answer := 42

// Creates and assigns
var answer = 42
Holdfast answered 19/1, 2014 at 10:12 Comment(0)
S
10

Another interpretation from outside the world of programming languages comes from Wolfram Mathworld, et al:

If A and B are equal by definition (i.e., A is defined as B), then this is written symbolically as A=B, A:=B, or sometimes A≜B.

http://mathworld.wolfram.com/Defined.html

https://math.stackexchange.com/questions/182101/appropriate-notation-equiv-versus

Staffard answered 28/3, 2016 at 19:16 Comment(0)
S
6

Some language uses := to act as the assignment operator.

Sanchez answered 17/3, 2011 at 20:18 Comment(2)
@Pacerier see this post #7462822 as to why the answer to your question can be both "Yes" and "No".Stickle
@TigOldBitties, Good gotcha from Erwin down there.Zaremski
E
3

In a lot of CS books, it's used as the assignment operator, to differentiate from the equality operator =. In a lot of high level languages, though, assignment is = and equality is ==.

Escapement answered 17/3, 2011 at 20:19 Comment(0)
H
1

This is old (pascal) syntax for the assignment operator. It would be used like so:

a := 45;

It may be in other languages as well, probably in a similar use.

Heterotrophic answered 17/3, 2011 at 20:19 Comment(0)
B
1

A number of programming languages, most notably Pascal and Ada, use a colon immediately followed by an equals sign (:=) as the assignment operator, to distinguish it from a single equals which is an equality test (C instead used a single equals as assignment, and a double equals as the equality test).

Reference: Colon (punctuation).

Barcot answered 24/8, 2021 at 6:15 Comment(0)
G
1

In Python:

Named Expressions (NAME := expr) was introduced in Python 3.8. It allows for the assignment of variables within an expression that is currently being evaluated. The colon equals operator := is sometimes called the walrus operator because, well, it looks like a walrus emoticon.

For example:

if any((comment := line).startswith('#') for line in lines):
    print(f"First comment: {comment}")
else:
    print("There are no comments")

This would be invalid if you swapped the := for =. Note the additional parentheses surrounding the named expression. Another example:

# Compute partial sums in a list comprehension
total = 0
values = [1, 2, 3, 4, 5]
partial_sums = [total := total + v for v in values]
# [1, 3, 6, 10, 15]
print(f"Total: {total}")  # Total: 15

Note that the variable total is not local to the comprehension (so too is comment from the first example). The NAME in a named expression cannot be a local variable within an expression, so, for example, [i := 0 for i, j in stuff] would be invalid, because i is local to the list comprehension.

I've taken examples from the PEP 572 document - it's a good read! I for one am looking forward to using Named Expressions, once my company upgrades from Python 3.6. Hope this was helpful!

Sources: Towards Data Science Article and PEP 572.

Genni answered 8/12, 2021 at 22:43 Comment(0)
S
0

It's like an arrow without using a less-than symbol <= so like everybody already said "assignment" operator. Bringing clarity to what is being set to where as opposed to the logical operator of equivalence.

In Mathematics it is like equals but A := B means A is defined as B, a triple bar equals can be used to say it's similar and equal by definition but not always the same thing.

Anyway I point to these other references that were probably in the minds of those that invented it, but it's really just that plane equals and less that equals were taken (or potentially easily confused with =<) and something new to define assignment was needed and that made the most sense.

Historical References: I first saw this in SmallTalk the original Object Language, of which SJ of Apple only copied the Windows part of and BG of Microsoft watered down from them further (single threaded). Eventually SJ in NeXT took the second more important lesson from Xerox PARC in, which became Objective C.

Well anyway they just took colon-equals assiment operator from ALGOL 1958 which was later popularized by Pascal

https://en.wikipedia.org/wiki/PARC_(company)

https://en.wikipedia.org/wiki/Assignment_(computer_science)

Assignments typically allow a variable to hold different values at different times during its life-span and scope. However, some languages (primarily strictly functional) do not allow that kind of "destructive" reassignment, as it might imply changes of non-local state. The purpose is to enforce referential transparency, i.e. functions that do not depend on the state of some variable(s), but produce the same results for a given set of parametric inputs at any point in time.

https://en.wikipedia.org/wiki/Referential_transparency

Spheroidal answered 3/6, 2018 at 9:59 Comment(0)
C
0

For VB.net,

a constructor (for this case, Me = this in Java):

Public ABC(int A, int B, int C){
Me.A = A;
Me.B = B;
Me.C = C;
}

when you create that object:

new ABC(C:=1, A:=2, B:=3)

Then, regardless of the order of the parameters, that ABC object has A=2, B=3, C=1

So, ya, very good practice for others to read your code effectively

Crinkumcrankum answered 5/2, 2020 at 7:58 Comment(0)
A
-1

Colon-equals was used in Algol and its descendants such as Pascal and Ada because it is as close as ASCII gets to a left-arrow symbol.

The strange convention of using equals for assignment and double-equals for comparison was started with the C language.

In Prolog, there is no distinction between assignment and the equality test.

Attentive answered 14/2, 2016 at 23:31 Comment(3)
If they wanted it close to the left arrow, they could have used <- like Haskell did. They weren't trying to get close to the left arrow with :=, they were using the mathematical 'is defined as' operator: mathworld.wolfram.com/Defined.htmlVolsung
Pedant alert: <- in Haskell is not assignment. Haskell does not have destructive assignment in the way of Pascal, Ada etc. <- is part of the do-notation syntax for parameter substitution. It is more analogous to the process of substituting values into parameters in a subroutine call.Attentive
@Michael Fair enough. You're right. My bad. Anyway, the point remains that if they were trying to imitate the left arrow, they would not have used :=, they would have used <-.Volsung

© 2022 - 2024 — McMap. All rights reserved.