I use the Scala circe library to convert objects of the case class Message
to JSON and also to create Message
objects from their JSON representation. This class is implemented as follows:
import io.circe
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.parser
import io.circe.syntax._
object Message {
implicit val measurementDecoder = deriveDecoder[Message]
implicit val measurementEncoder = deriveEncoder[Message]
def fromJson(jsonString: String): Either[circe.Error, Message] =
parser.decode[Message](jsonString)
}
case class Message(id: Int, text: String) {
def toJson() = (this).asJson.noSpaces
def toJson2() = this.asJson.noSpaces // syntax error: No implicit arguments of type: Encoder[Message.this.type]
}
My point is the implementation of the method toJson
. While this variant works
def toJson() = (this).asJson.noSpaces
the variant
def toJson() = this.asJson.noSpaces
leads to the syntax error
No implicit arguments of type: Encoder[Message.this.type]
So what's the difference between this
and (this)
in Scala?
As pointed out by Luis Miguel Mejía Suárez it is neither a syntax nor a compile error, but a bug in the current version of the IntelliJ IDEA Scala plugin (version 2021.3.17). Using parentheses is a way to get around this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With