In Scala, is assignment operator “=” a method call?

后端 未结 3 1439
后悔当初
后悔当初 2020-11-28 15:18

As per Scala book, \"Programming In Scala\" -

Scala is an object-oriented language in pure form: every value is an object and every operation is a method call

相关标签:
3条回答
  • 2020-11-28 15:41

    A little addition to Jatin answer. There is one case when = can be considered as a method call, but actually it's just a syntactic sugar. In OO part of Scala, where ugly vars lives, you can write the following:

    class Test { 
      private var x0: Int = 0
      def x = x0
      def x_=(a: Int) = x0 = a 
    }
    

    Then you can assign new ints to x:

    scala> val t = new Test
    t: Test = Test@4166d6d3
    
    scala> t.x = 1
    t.x: Int = 1
    

    The last line will be desugared into t.x_=(1). I think in this case, considering syntactic sugar, it's possible to say that = is a method call.

    0 讨论(0)
  • 2020-11-28 15:42

    While the other answers are correct for standard Scala, there is a variant called Scala-Virtualized where = and other control structures are method calls.

    0 讨论(0)
  • 2020-11-28 15:45

    Nope. Assignment operator (=) is a reserved word. Also the below are:

    _ : = => <- <: <% >: # @

    For a more comprehensive list refer § 1.1. Some more information regarding = is described in § 6.12.4.

    So yes it is not a method call.

    0 讨论(0)
提交回复
热议问题