0

I am reading the source code of the class kafka.core.log.LogSegment. Where the syntax of scala gives me huge confusion. I know I could make it clear if I can learn scala in a systematic way But I just don't have that much time since my project awaits.

Definition of the methods:

@volatile private var _maxTimestampSoFar: Option[Long] = None//**#pos 0 constructor??**
  def maxTimestampSoFar_=(timestamp: Long): Unit = _maxTimestampSoFar = Some(timestamp)//**definition 1**
  def maxTimestampSoFar: Long = {//**definition2**
    if (_maxTimestampSoFar.isEmpty)
      _maxTimestampSoFar = Some(timeIndex.lastEntry.timestamp)
    _maxTimestampSoFar.get
  }

Where they are called:

  if (largestTimestamp > maxTimestampSoFar) {//**#pos 3.getter**
        maxTimestampSoFar = largestTimestamp//**#pos4 set the value?**
        offsetOfMaxTimestampSoFar = shallowOffsetOfMaxTimestamp
      }

What confuses me can be concluded into the following:

  1. What is usage of this kind of method with an extra "_" after the identifier of the method like this one here: the maxTimestampSoFar_. When I checked the usage of definition 1 and definition 2, there occurrence overlaps, from which can I conclude they are regarded as the same method like overloaded twins?But since they have different parameters, why we need a difference in the identifier?
  2. As for the place the method is called, is my understanding correct? Is pos 4 the place where definition 1 of the method is called? Then the argument of the parameter is passed just by using the "=" ?
  3. If the second assumption is correct, then the at above pos 0, is it the call of Option's constructor? This is like calling the default constructor?

Hope anyone can help me. Appreciate that.

1 Answer 1

2
  1. The method name contains also the equals sign, so is maxTimestampSoFar_=. That is how setters are defined in Scala (see Scala getters/setters - best practice?)
  2. Yes, what looks like an assignment in pos 4 will invoke the method defined in 1
  3. Option[Long] can either contain None or Some(<long value>), pos 0 in the code initializes the variable with value None
Sign up to request clarification or add additional context in comments.

3 Comments

"The method name contains also the equals sign". What does the equal sign do with getter or setter? I noticed that the setter has an extra equal sign is, is this a coding convention or due to some reason else?
I am still not confident about the inner logic. ` def maxTimestampSoFar_=(timestamp: Long): Unit = _maxTimestampSoFar = Some(timestamp)`. So can I think it the way that when the setter is called at pos4, it goes one equal sign down to wait the argument and when it get a long, it go further down to the assignment of the field?
The assingment maxTimestampSoFar = value is translated to an invocation maxTimestampSoFar_=(value) That's a Scala naming convention: docs.scala-lang.org/style/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.