Tuesday 15 February 2011

sbt - Scala initialisation order (using scalatest) -


If someone could explain to me why problems arise due to the following reasons, I would be grateful. I have set a small example to display this issue:

I have a project that I am building with SBT, build.sbt is being done as follows:

  lazy well root = (project (".") Settings in the file (name: = "test", version: = "1.0.0", skulverson: = "2.11.5", librarydescensions + = "Org.scalatest" %% "skelete"% "2.2.4"% test)  

and test.scala in src / main / scala:

 < Code> object test app G. Val Map = Map (1 -> 2) Def Access = / (1) println (access)}  

and testSpec.scala (using scalest Tax):

  import org.scalatest .FlatSpec class extends TestSpec FlatSpec to {{test} in <{code}  

("test.access == 2 ")" {{test} "{" A test "is allowed to" access the map ") If I run the sbt run then '2' is printed as expected. I get a NullPointerException and then the test fails:

  [info] TestSpec: [info] A Test [information] - should allow entry to the map * *** *** [information] java.lang.NullPointerException: $ info $ test on $ info.access (Test.scala: 4) [info] TestSpec $$ $ $ 1.apply $ mcv $ sp (TestSpec Scala: 7) etc.  

This is because the map is zero when Test speech accesses it. I can fix this by turning the scum in the form of lazy wallets or def, but I would like to know a bit more about what is happening with the initial order and how can I survive this kind of thing in the future. . If I try to access it from another source file in src \ main \ scala, then I have no problem, and to fix this kind of problem, I would like to avoid changing the definitions of my variables like this .

Thank you

I did not know the answer, but interesting to do research Was there.

Apparently the specialty app is a bit special

It should be noted that this property has been implemented using the DelayedInit functionality , Which means that the field will not be initialized before the main method is executed.


No comments:

Post a Comment