def flatMap[B](f: A => IO[B]): IO[B] = new:
def unsafeRun = f(self.unsafeRun).unsafeRun
This method creates a new IO object
I don't understand the use of new without a type. Feels like structural type, but not quite sure. I'm Just starting to transition to scala 3.
What you described is a new feature of Scala 3, which allows you to drop type name when creating an anonymous instance of some class if the type can be inferred:
While it feels like a structural typing it's a nominal typing with type inference extended to anonymous classes created with
newkeyword.You could even use this in some nested contexts like:
but it becomes unreadable almost immediately, which is why this presentation classifies it as an Scala-3-specific antipattern.
It might be a really nice way to quickly to hack some one-time-use script, but please avoid using it in larger long-living codebases.