Search code examples
scalasyntaximplicitscala-3given

Given member definitions starting with `with` are no longer supported


Given the following code (pun not intended):

trait Display[A]:
  def display(value: A): String


object Display:
  given stringDisplay: Display[String] with
    def display(input: String) = input

IntelliJ displays the following warning:

Given member definitions starting with with are no longer supported; use {...} or : followed by newline instead

This is odd because the official documentation still shows given...with, and I can't find anything corresponding to this message online.

Furthermore, I don't know how to apply the suggestion, because none of the following compiles:

given stringDisplay: Display[String] {
    def display(input: String) = input
}

given stringDisplay: Display[String]:
  def display(input: String) = input

What would be the correct syntax to apply the suggestion from IntelliJ? Using Scala 3.6.2.


Solution

  • After digging through Scala change logs and commits, I found commit 14acdc0 to support SIP-64, which changes context bound and given clause syntaxes.

    The new syntax is to replace the with with a :, exactly as suggested by the message. However, IntelliJ CE 2024.3.1.1 doesn't recognize this syntax, neither does scalameta. I've opened tickets for each.

    scalameta: https://github.com/scalameta/scalameta/issues/4090

    IntelliJ: https://youtrack.jetbrains.com/issue/SCL-23372/Fails-to-recognize-SIP-64-syntax