Can an implicit value also be a lazy value when using an implicit method as a parameter value?

I have a test suite that uses Slick access to access DB. Some tests in this package have access to the database, and some do not. My package has

implicit val db = DB.getDB

which effectively initializes DataBaseDefat the start of the package. This value is then used as the implicit parameter value for some methods. It also has afterAll()one that closes dbat the end of the package:

override def afterAll():Unit={
    db.close()
    super.afterAll()
  }

Now, if I changed to: implicit lazy val db = DB.getDB what exactly will happen?

If I run only those tests that do not use the database, the connection will not be initialized, but afterAll()it will still try to close the connection, and I have a problem in this case, right? I tried to start, but no error occurred, and there were no exceptions ...

My knowledge of the residuals is not enough to help me understand this in combination with the lazy.

+4
source share
2 answers

what exactly will happen?

The value will not be initialized until the first access to it.

it will still try to close the connection, and I have a problem in this case, right?

db.close() , , DB.getDb . , , , , , .

+7

, , , val lazy val def .

, , , , , ; , , , , . , , ( , ).

, , , , memoized , val, memoized , lazy val , def. , , .

+1

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


All Articles