Skip to content

Instantly share code, notes, and snippets.

@bvenners
Created October 20, 2014 17:24
Show Gist options
  • Save bvenners/eb4d413d007e67f84102 to your computer and use it in GitHub Desktop.
Save bvenners/eb4d413d007e67f84102 to your computer and use it in GitHub Desktop.
Typesafe contains and *indexOf* methods on covariant Seq-like class
//
// This demos type errors for contains, indexOf, lastIndexOf, indexOfSlice, and lastIndexOfSlice
// on org.scalactic.Every, which is a Seq-like object: a covariant ordered collection. (It does
// not extend scala.collection.immutable.Seq because it enforces an invariant of non-emptiness, which
// is incompatible with Seq.) These methods are declared on Every, not added with an implicit conversion.
// For example, here is the declaration of contains:
//
// https://github.com/scalatest/scalatest/blob/release-3.0.0-SNAP1-for-scala-2.11-and-2.10/src/main/scala/org/scalactic/Every.scala#L299
//
// Scaladoc for this version of Every is here:
//
// http://www.artima.com/docs-scalatest-3.0.0-SNAP1/#org.scalactic.Every
//
scala> import org.scalactic._
import org.scalactic._
scala> Every(1, 2, 3).contains(2)
res0: Boolean = true
scala> Every(1, 2, 3).contains("two")
<console>:11: error: Could not find evidence that String can be contained in org.scalactic.Every[Int];
the missing implicit parameter is of type org.scalactic.enablers.SafeSeqsConstraint[org.scalactic.Every[Int],String]
Every(1, 2, 3).contains("two")
^
scala> Every(1, 2, 3).indexOf(2)
res2: Int = 1
scala> Every(1, 2, 3).indexOf("two")
<console>:11: error: Could not find evidence that Any can be contained in org.scalactic.Every[Int];
the missing implicit parameter is of type org.scalactic.enablers.SafeSeqsConstraint[org.scalactic.Every[Int],Any]
Every(1, 2, 3).indexOf("two")
^
scala> Every(1, 2, 3).lastIndexOf(2)
res4: Int = 1
scala> Every(1, 2, 3).lastIndexOf("two")
<console>:11: error: Could not find evidence that Any can be contained in org.scalactic.Every[Int];
the missing implicit parameter is of type org.scalactic.enablers.SafeSeqsConstraint[org.scalactic.Every[Int],Any]
Every(1, 2, 3).lastIndexOf("two")
^
scala> Every(1, 2, 3).indexOfSlice(Every(2, 3))
res6: Int = 1
scala> Every(1, 2, 3).indexOfSlice(Every("two", "three"))
<console>:11: error: Could not find evidence that Any can be contained in org.scalactic.Every[Int];
the missing implicit parameter is of type org.scalactic.enablers.SafeSeqsConstraint[org.scalactic.Every[Int],Any]
Every(1, 2, 3).indexOfSlice(Every("two", "three"))
^
scala> Every(1, 2, 3).lastIndexOfSlice(Every(2, 3))
res8: Int = 1
scala> Every(1, 2, 3).lastIndexOfSlice(Every("two", "three"))
<console>:11: error: Could not find evidence that Any can be contained in org.scalactic.Every[Int];
the missing implicit parameter is of type org.scalactic.enablers.SafeSeqsConstraint[org.scalactic.Every[Int],Any]
Every(1, 2, 3).lastIndexOfSlice(Every("two", "three"))
^
// This is the build.sbt I used to generate the above REPL session. Just
// place this in a directory and say:
// $ sbt
// ...
// > console
scalaVersion := "2.11.2"
libraryDependencies += "org.scalactic" % "scalactic_2.11" % "3.0.0-SNAP1"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "3.0.0-SNAP1" % "test"
initialCommands in console := "import org.scalactic._"
initialCommands in Test in console := """|import org.scalatest._
|import org.scalactic._
|import Matchers._""".stripMargin
resolvers += "Sonatype OSS Releases" at "https://oss.sonatype.org/content/repositories/releases"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment