What does “:=” do?

前端 未结 10 1732
别那么骄傲
别那么骄傲 2020-12-04 10:47

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

10条回答
  •  囚心锁ツ
    2020-12-04 11:15

    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

提交回复
热议问题