5

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?

  • what is the error you are getting ? – Amit Prasad Dec 22 '21 at 09:59
  • The syntax error is: `No implicit arguments of type: Encoder[Message.this.type]`, but only if the parens around `this`are omitted. – Johann Heinzelreiter Dec 22 '21 at 10:55
  • Now I am confused. In the first version of this question, you had a compile error. Now, it is a syntax error. Those are two very different things. Which of the two is it? In particular, the *text* of the error message does not sound like a syntax error. – Jörg W Mittag Dec 22 '21 at 12:59
  • 5
    I can't reproduce it: https://scastie.scala-lang.org/BalmungSan/yBZzq0deQ5i8QOW87R5puA/2 - Which means that either the error is not real, is only some bug in your IDE. Or, the code you shared is not enough to reproduce the error; please create an **Scastie** that actually reproduces the error and then edit the question. – Luis Miguel Mejía Suárez Dec 22 '21 at 13:28

1 Answers1

2

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.