scala/scala3-example-project

Example usage @threadUnsafe annotation

Teinnsei opened this issue · 2 comments

Hi everyone.

This project include example to use lazy val field. In new proposal to Scala project have changed implementation of lazy val.

    /**
      * In order to get thread safety, you need to put @volatile before lazy vals.
      * https://dotty.epfl.ch/docs/reference/changed-features/lazy-vals-init.html
      */
    @volatile lazy val xs: List[String] = List("d", "o", "t", "t", "y")

As I understood to safe thread-safe sematics we need to attach @volatile annotation to lazy val field.

Dotty/Scala3 in std have scala.annotation.threadUnsafe annotation also. Is it still actual annotation? And if it is true in wich cases we need to use this annotation. Can you please add example?

Thank you

iirc, early Dotty versions had thread-unsafe lazy vals. but at some point the design changed and it was decided to make thread-safety the default again, as in Scala 2. so yeah, a fix is needed here.

Thank you for your explanation.