Hacker News new | ask | show | jobs
by kibibu 2905 days ago
High-level overview: it's an assignment operator that returns its value, similar to C's assignment operator.

The choice of := is to avoid accidentally using assignment where comparison is expected.

2 comments

I feel the colon is unnecessary, especially considering how C deals with this. A plain '=' inside a conditional is already invalid syntax in Python.
And it's a very well-known source of bugs in C, since it's to close to "==". I don't think new languages adopting that is a good idea.
Sure. But if fidelity to C style was not a concern then I don't see why the '==' syntax was adopted in the first place.
== is an incredibly common syntax for equality and stand-alone not a problem. only if you introduce = to expressions too it becomes a risk. (well, you could theoretically accidentally write == for a normal assignment, but that kind of error is caught more easily)
No, it's necessary.
How so? Syntactically, or from a pragmatic point of view?
Yeah but there is already solution for that in C: put parenthesis around assignment when using its value as bool. The compilers warn if you don't so making this error in C can only happen if you don't use warnings.