Search code examples
scalainheritanceargumentsscalding

Scalding overriding args in subclass


I have two Scalding jobs, where one inherits from the other. Something like this

class BaseJob(args : Args) extends Job(args) {
   val verbose = args.boolean("verbose")

   if(verbose){
     // do stuff
   }else{
     // do other stuff
   }
}

class MyJob(args : Args) extends BaseJob(args) {
  override val verbose = true

  override def formatOutput(p: Pipe): Pipe = {
    println(verbose) // outputs false if not run with --verbose
    p.project('field1, 'field2, 'field3)
  }
}

The idea is that some code in MyJob assume that we are running with the verbose flag, so instead of making this a parameter, I want to make sure this is always set. However simply overriding the class variable does not work, since it will still take the value of 'verbose' from the command line parameter. Does anyone know how this could be done?


Solution

  • It can be done by having an extra 'extends'. This way the inheritance does work as expected.

    class MyJob(args : Args) extends { override val verbose = true } with BaseJob(args) {
    
      override def formatOutput(p: Pipe): Pipe = {
        println(verbose) // outputs false if not run with --verbose
        p.project('field1, 'field2, 'field3)
      }
    }