Skip to content

Instantly share code, notes, and snippets.

View etorreborre's full-sized avatar
🏠
Working from home

Eric Torreborre etorreborre

🏠
Working from home
View GitHub Profile
/**
* This doesn't compile
* type mismatch;
* found : Iterable[T]
* required: CC[T]
* def dropOne: CC[T] = xs.drop(1)
*/
object o {
implicit def extend[T, CC[T] <: Iterable[T]](xs: CC[T]) = new ExtendedIterable(xs)
package test
import org.specs.mock.Mockito
import org.scalatest.WordSpec
import org.specs.specification._
/**
* DefaultExampleExpectationsListener provides default behavior for specs creation of
* examples and expectations.
*
// a Map can be used as a PartialFunction
List("Darth Vador", "Eric", "Frankenstein") collect Map("Eric" -> "good", "Darth Vador" -> "very evil", "Frankenstein" -> "evil") must_== List("very evil", "good", "evil")
@etorreborre
etorreborre / gist:730097
Created December 6, 2010 10:17
An implicit pearl in Scala
/**
* An answer from Jason Zaugg on the scala-internals mailing list
*
* Implicit parameters can have defaults.
*/
scala> implicit def OptionalImplicit[A <: AnyRef](implicit a: A = null) = Option(a)
OptionalImplicit: [A <: AnyRef](implicit a: A)Option[A]
scala> implicitly[Option[Ordering[Int]]]
/**
* An example showing why changing a parameter to "call by name" may break clients of an API.
* As usual, side-effects are the main issue here,...
*/
/** "before" api */
def registerDatabaseState(state: DatabaseState) = // store an event with the current state of the database
/**
* "after" api. Storing the event may not happen based on the configuration for example
@etorreborre
etorreborre / gist:812358
Created February 5, 2011 10:35
specs compilation error with the latest snapshot
[INFO] Compiling 152 source files to C:\projects\specs\specs-2.9.0\target\test-classes at 1296871592783
[INFO] java.lang.reflect.InvocationTargetException
[INFO] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[INFO] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[INFO] at java.lang.reflect.Method.invoke(Method.java:597)
[INFO] at org_scala_tools_maven_executions.MainHelper.runMain(MainHelper.java:161)
[INFO] at org_scala_tools_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
[ERROR] Caused by: java.lang.AssertionError: assertion failed
[INFO] at scala.Predef$.assert(Predef.scala:85)
@etorreborre
etorreborre / gist:843406
Created February 25, 2011 05:33
Workaround for this?
"" size
1
error: scala.this.Predef.augmentString("").size of type Int does not take parameters
"" size
@etorreborre
etorreborre / gist:848699
Created March 1, 2011 06:10
How to opt out of implicits inherited from traits
// a trait defining a transformation method
trait A {
case class Other(s: String)
def toOther(s: String) = Other(s)
}
// the same trait with the method defined as implicit
trait AB extends A {
override implicit def toOther(s: String) = super.toOther(s)
}
@etorreborre
etorreborre / gist:848720
Created March 1, 2011 06:28
How to opt out from an inherited implicit - 2
// base functionality
trait A {
case class Other(s: String) {
def in: String = "in"
}
def toOther(s: String) = Other(s)
}
// base functionality with an implicit
trait AB extends A {
override implicit def toOther(s: String) = super.toOther(s)
@etorreborre
etorreborre / gist:904913
Created April 6, 2011 00:49
A complete example of using Around
/**
* For those examples using Around may not be very different from a simple method call with a call-by-name
* parameter.
*
* The only difference is that the http object is a named context that you can compose with other setups, like a Before setup:
*
* http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Composing+contexts
*/
class AcceptanceSpec extends Specification { def is =