What is the difference between = and := in Scala?

后端 未结 4 1035
一个人的身影
一个人的身影 2020-12-24 00:44

What is the difference between = and := in Scala?

I have googled extensively for \"scala colon-equals\", but was unable to find anything de

4条回答
  •  庸人自扰
    2020-12-24 01:16

    from Martin Odersky:

    • Initially we had colon-equals for assignment—just as in Pascal, Modula, and Ada—and a single equals sign for equality. A lot of programming theorists would argue that that's the right way to do it. Assignment is not equality, and you should therefore use a different symbol for assignment. But then I tried it out with some people coming from Java. The reaction I got was, "Well, this looks like an interesting language. But why do you write colon-equals? What is it?" And I explained that its like that in Pascal. They said, "Now I understand, but I don't understand why you insist on doing that." Then I realized this is not something we wanted to insist on. We didn't want to say, "We have a better language because we write colon-equals instead of equals for assignment." It's a totally minor point, and people can get used to either approach. So we decided to not fight convention in these minor things, when there were other places where we did want to make a difference.

    from The Goals of Scala's Design

提交回复
热议问题