mercredi 4 mai 2022

How can I execute common code post every method in scala

I have a base trait which contains some operations

trait BaseOperations {

  def read(path: String): Data
  
  def write(path: String, data: Data): Boolean

  def exists(path: String): Boolean

}

Now I have specific implementations which extends this base trait. As part of metric collection I want to count how many times any method in the trait is called. Current implementation is:

object LocalFileSystem extends BaseOperations {

  var counter: Int = 0
  
  override def read(path: String): Data = {
    //do something
    counter+=1
    return data
  }

}

Instead of incrementing for every method call in every specific implementation is there any better approach I can take by moving this to trait

Aucun commentaire:

Enregistrer un commentaire