1

I'm quite confusing on how can ScalaMeter chain configuration like this

val standardConfig = config(
  Key.exec.minWarmupRuns -> 5,
  Key.exec.maxWarmupRuns -> 10,
  Key.exec.benchRuns -> 10,
  Key.verbose -> true
) withWarmer(new Warmer.Default)

The first config(...) expression returns MeasureBuilder[T, U] type, this I understand.

However, how can we chain the second expression withWarmer(new Warmer.Default) which also returns MeasureBuilder type.

At first, I guess that MeasureBuilder type implement apply method that allow us to do this, but at the last step before measuring the performance of a piece of code we need

val partime = standardConfig measure {
  ...
}

where measure { ... } return Quantity[U] type which is not MeasureBuilder.

So, How can ScalaMeter configuration chain the expression like that?

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • 3
    The space between `config(..)` and `withWarmer` is just an alternative syntax to the dotted one, similar to what would be `config(..).withWarmer(..)` – cchantep Jan 04 '21 at 07:07
  • 1
    You should change your `measure` so that it returns the Builder. Or better yet, create a helper `withMeasure` that delegates to the existing implementation and then return to the Builder. You could add it using an implicit if `MeasureBuilder` is a third party type – Aluan Haddad Jan 04 '21 at 07:26

1 Answers1

2

Please first note that config method is exposed after imporing:

import org.scalameter.config

This is because it is declared on the companion object in the package level:

package object scalameter extends MeasureBuilder[Unit, Double](

Therefore when you declare val standardConfig = config(...) you get MeasureBuilder[Unit, Double]

Afterwards, the class MeasureBuilder exposes a method withWarmer, which is actually called, and returns a MeasureBuilder[Unit, Double], with both configuration and warmer applied.

MeasureBuilder exposes a method measure:

def measure[S](b: =>S): Quantity[U] = measured(b)._2

which returns Quantity[Double] in your example.

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35