IntelliJ unable to resolve certain types, ex: EnumValue on a Mongo record - Scala

381 Views Asked by At

For Example:

object CampaignTypes extends Enumeration {

  type CampaignType = Value

  val ABC,DEF = Value
}

object campaignTypeId extends EnumNameField(this, CampaignTypes) {
    override val defaultValue = CampaignTypes.ABC
  }

IntelliJ underlines CampaignTypes.ABC in red with message Expression of type CampaignTypes.Value doesn't conform to expected type EnumType#Value

The code compiles & works. But, IntelliJ marks it as an error, making it difficult to read code (as there are many other cases, which is also not resolved by IntelliJ). The right Scala plugin is also used. Is there a way to resolve this?

Another example w.r.t methods defined on a BsonRecord

sealed trait Product {...}

class Document extends BsonRecord[Document] {
  object productType extends StringField(this, 20)
  ....
  def toTyped: Option[Product] = this.productType.get match {//something which returns an Option[Product] from a List[Product]}
}

object documents extends BsonRecordListField(this, Document) {
    def toProducts: Set[Product] =
      this.get.flatMap(_.toTyped)(breakOut) //Cannot resolve symbol toTyped
}
1

There are 1 best solutions below

2
On

Yes, you can help Intellij by providing type hints :

object campaignTypeId extends EnumNameField[A, CampaignsTypes.type](this, CampaignTypes) {
    override val defaultValue = CampaignTypes.ABC
  }

where A is the type of the encapsulating class/object

For a general solution :

  • Try to always provide generic types instead of relying on inference, as you can see the one in Intellij IDEA is not great in diffucult cases.
  • If that doesn't cut it, try to provide compiler hints. Lift relies on Manifest, but it can be classTag or other things. Expliciting this kind of implicits can help IDEA resolve types correctly

For BsonRecordListField, expliciting generic types should solve it too.