Skip to content

Commit

Permalink
Fix 'cannot cast to Nothing'
Browse files Browse the repository at this point in the history
Exception: class java.util.LinkedHashMap cannot be cast to class scala.runtime.Nothing$
  • Loading branch information
carymrobbins committed Jul 18, 2022
1 parent ad1f507 commit b8e3c75
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions jps-shared/src/com/haskforce/importWizard/stack/StackYaml.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.haskforce.importWizard.stack

import com.intellij.openapi.util.io.FileUtil
import scalaz.\/
import scalaz.{-\/, \/}
import scalaz.std.list._
import scalaz.syntax.either._
import scalaz.syntax.traverse._

import java.io.File
import java.util
import scala.collection.JavaConverters._
import scala.util.control.NonFatal

/**
* The parse result of a stack.yaml file.
Expand Down Expand Up @@ -64,16 +65,19 @@ object StackYamlUtil {
}
}


/** A wrapper around snakeyaml to provide a safer interface. */
object Yaml {

final case class Error(message: String)

def parse(doc: String): Error \/ Yaml = {
\/.fromTryCatchNonFatal(new org.yaml.snakeyaml.Yaml().load(doc))
.leftMap(e => Error(e.getMessage))
.flatMap(fromObject)
try {
// The ':Object' type annotation is needed to prevent inferring 'Nothing'
// which triggers a class cast exception. Seems to be a bug in scalac 2.12.4.
fromObject(new org.yaml.snakeyaml.Yaml().load(doc): Object)
} catch {
case NonFatal(e) => -\/(Error(e.getMessage))
}
}

def fromObject(o: Any): Error \/ Yaml = o match {
Expand Down

0 comments on commit b8e3c75

Please sign in to comment.