This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This demos type errors for safe variants of contains, indexOf, lastIndexOf, indexOfSlice, and lastIndexOfSlice | |
// added to Scala Seqs and Arrays via an implicit conversion. These do rely on the trick of implicitly converting | |
// away the covariance of the type parameter, because in Scala 2.10 that is necessary. But in Scala 2.11, it is not | |
// necessary, because the compiler will disinguish beween implicits for covariant and invariant Seqs. Nevertheless | |
// I think provides value so I've done it this way by commenting out the implicit that doesn't work in 2.10: | |
// | |
// https://github.com/scalatest/scalatest/blob/master/src/main/scala/org/scalactic/SafeSeqs.scala#L25 | |
// | |
// Someday when we drop support for Scala 2.10 I can uncomment that implicit and then this will no longer be |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This gist demos the EquaSet type coming in Scalactic 3.0, which is a set that | |
// can use an alternate equality for determining set membership. | |
// | |
// The Scaladoc for this is version of Scalactic here: | |
// | |
// http://www.artima.com/docs-scalatest-3.0.0-SNAP1/#package | |
// | |
// A screencast that includes an EquaSets demo is here: | |
// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import org.scalactic._ | |
import org.scalactic._ | |
scala> def reasons(x: Int): Int Or ErrorMessage = | |
| for { | |
| y <- Good(x) filter { i => if (i > 0) Pass else Fail(s"$i was <= 0") } | |
| z <- Good(y / 2) filter { i => if (i > 1) Pass else Fail(s"$y / 2 was <= 1") } | |
| } yield z | |
reasons: (x: Int)org.scalactic.Or[Int,org.scalactic.ErrorMessage] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> console | |
[info] Compiling 1 Scala source to /Users/bv/nobkp/delus/redlady/target/scala-2.11/classes... | |
[info] Starting scala interpreter... | |
[info] | |
import org.scalatest._ | |
import org.scalactic._ | |
import Matchers._ | |
Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65). | |
Type in expressions to have them evaluated. | |
Type :help for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() | |
{ | |
if (1 == 1L) | |
printf("In C, 1 == 1L\n"); | |
else | |
printf("In C, 1 != 1L\n"); | |
if (1L == 1) | |
printf("In C, 1L == 1\n"); | |
else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import CheckedEquality._ | |
import CheckedEquality._ | |
scala> 1 === "one" | |
<console>:20: error: types Int and String do not adhere to the type constraint selected for the === and !== operators; the missing implicit parameter is of type org.scalactic.EqualityConstraint[Int,String] | |
1 === "one" | |
^ | |
scala> "one" === 1 | |
<console>:20: error: types String and Int do not adhere to the type constraint selected for the === and !== operators; the missing implicit parameter is of type org.scalactic.EqualityConstraint[String,Int] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import scala.reflect.runtime.universe._ | |
import scala.reflect.runtime.universe._ | |
scala> def countImplicits(tp: Type): Int = tp.members.count(_.isImplicit) | |
countImplicits: (tp: reflect.runtime.universe.Type)Int | |
scala> countImplicits(typeOf[org.specs2.mutable.Specification]) | |
res0: Int = 205 | |
scala> countImplicits(typeOf[org.scalatest.Spec]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import org.scalactic._ | |
import org.scalactic._ | |
scala> val lower = EquiSets[String](StringNormalizations.lowerCased.toHashingEquality) | |
lower: org.scalactic.EquiSets[String] = EquiSets(NormalizingHashingEquality(lowerCased)) | |
scala> val trimmed = EquiSets[String](StringNormalizations.trimmed.toHashingEquality) | |
trimmed: org.scalactic.EquiSets[String] = EquiSets(NormalizingHashingEquality(trimmed)) | |
scala> val hiho = lower.EquiSet("hi", "ho") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> implicit class BVList[A](xs: List[A]) { | |
| def :=:[B >: A](x: B)(implicit ev: B =:= A): List[B] = x :: xs | |
| def containz(elem: A): Boolean = xs.contains(elem) | |
| } | |
defined class BVList | |
scala> val xs = List(1, 2, 3) | |
xs: List[Int] = List(1, 2, 3) | |
scala> 5.0 :: xs |