0

IntelliJ gives me the warning that "abstract value in trait may cause errors during initialization" - as shown in the picture below.

  • What does this mean ?
  • Should I take this warning seriously ?
  • Should I use def-s instead of lazy val-s ?
  • Are they not the same in this case ? Since we are talking here about dependency injection, which creates a static - possibly cyclical - object graph.
  • But ! because the lazy modifier is there, it does not matter if the graph is cyclical or not. Right ?
  • The values themselves will simply behave as they were a memoized function right ?
  • Since we are talking about references here (in a static object graph) where references are directed edges and objects are nodes, what's the problem here ?
  • Could you please give an example where using lazy val-s instead of def-s can cause problems during initialization ?

Thank you for reading :)

The whole codebase is on github.

This is the particular line that is problematic (on github).

I also copy paste the problematic code here:

trait EntityMarshallers[V<:EntityValue[V]]{
  implicit lazy val decoderEntityV:    Decoder[Entity[V]]
  implicit lazy val encoderEntityV:    Encoder[Entity[V]]
  implicit lazy val _encoderV:         Encoder[V]
  implicit lazy val classTag:          ClassTag[V]

}

This is the IntelliJ warning :

enter image description here

jhegedus
  • 20,244
  • 16
  • 99
  • 167
  • 3
    If you try to compile your code you'll see that you get compile error "lazy values may not be abstract". – Dmytro Mitin Sep 08 '19 at 18:24
  • Try to make them `implicit def`. – Dmytro Mitin Sep 08 '19 at 18:29
  • 3
    Regarding initialisation warning see [Why is my abstract or overridden val null?](https://docs.scala-lang.org/tutorials/FAQ/initialization-order.html) and [possible duplicate](https://stackoverflow.com/q/7846119/5205022). – Mario Galic Sep 08 '19 at 18:40
  • @MarioGalic but strangely on this article one solution for that are `lazy val`s – pme Sep 09 '19 at 05:58

0 Answers0