Factoring Application Feature

I have an application that uses the App trait and everything works fine. But the object that inherits from the App becomes a little cumbersome, so I would like to separate some of the functionality into features. In particular, I would like to separate the processing of command line arguments.

Unfortunately, this does not look good with DelayedInit . So this works great:

 object Main extends App { println("arguments are: "+ args.mkString.mkString(", ")) } 

but this raises a NullPointerException :

 trait CommandLineArguments { this: App => println("arguments are: "+ args.mkString.mkString(", ")) } object Main extends App with CommandLineArguments 

Is there a way to get DelayedInit to "enable" mixed symptoms?

+4
source share
1 answer

It looks weird if you write something like this, it works fine:

 trait CommandLineArguments { self: App => delayedInit { println("arguments are: "+ args.mkString.mkString(", ")) } } 

But from scaladocs (http://www.scala-lang.org/api/current/index.html# scala.DelayedInit):

Classes and traits inheriting the DelayedInit marker will have their initialization code rewritten as follows. [Code] becomes delayedInit ([Code]) The initialization code includes all statements and all value definitions that are executed during initialization.

+3
source

Source: https://habr.com/ru/post/1380895/


All Articles