implicit

Implicit definition working for Seq but not for Set

元气小坏坏 提交于 2019-12-11 14:49:22
问题 So I've made some utility classes and implicit conversions for them. However, it works fine when converting from a Seq but not from a Set, although the code is the same, and those two traits seem rather similar at first sight. What could be the problem, and how would I fix it? import scala.collection.mutable.HashMap trait Indexed[T] { def index: T } class IndexMap[T, V <: Indexed[T]] extends HashMap[T, V] { override def put(key: T, value: V): Option[V] = { require(key == value.index) super

compiler failure to resolve Implicit types with bounds (sometimes)

空扰寡人 提交于 2019-12-11 13:27:17
问题 Edit : Last revision was deemed unhelpful as it did not contain necessary information that help narrow down my issue. hence the need to also include the AST. Below is a library in its entirety that allows parsing and writing of play-json 's json based on user defined schema; Similar to what Scala's slick offers for database columns to some extent: import scala.language.higherKinds import play.api.libs.functional.syntax._ import play.api.libs.json._ import scala.language.{higherKinds,

Scala - Optional Predicate

送分小仙女□ 提交于 2019-12-11 11:53:25
问题 I was informed to use this interesting piece of code, but my use case requires it to do a bit more than is currently possible. implicit class Predicate[A](val pred: A => Boolean) { def apply(x: A) = pred(x) def &&(that: A => Boolean) = new Predicate[A](x => pred(x) && that(x)) def ||(that: A => Boolean) = new Predicate[A](x => pred(x) || that(x)) def unary_! = new Predicate[A](x => !pred(x)) } Some use case boilerplate: type StringFilter = (String) => Boolean def nameFilter(value: String):

Play Framework / Scala: abstract repository and Json de/serialization

。_饼干妹妹 提交于 2019-12-11 10:18:49
问题 This question is maybe more about Scala than Play, but here it is: I am trying to achieve an abstraction of a repository for common DB operations. trait Entity { def id: UUID } trait Repository[T <: Entity] { val JSON_KEY_ID = "_id" def collection: JSONCollection def insert(t: T): Future[Either[String, T]] = { collection.insert(t).map(wr => if (wr.ok) Right(t) else Left(wr.getMessage())) .recover { case t => Left(t.getMessage) } } def update(t: T): Future[Either[String, T]] = { val selector =

Creating an implicit function that wraps map() in Scala with the right type: Not for the faint at heart

倾然丶 夕夏残阳落幕 提交于 2019-12-11 08:45:47
问题 I am trying to implement an implicit function mapMetered that wraps map and functions exactly like it in terms of returning the correct type. I tried this: implicit class MeteredGenTraversablePimp[T, C[T] <: GenTraversable[T]](trav: C[T]) { def foreachMetered(m: Meter)(f: T => Unit) = m.foreach(trav)(f) def mapMetered[B, That](m: Meter)(f: (T) => B)( implicit bf: CanBuildFrom[C[T], B, That] ): That = { m.start() try { trav.map { x => val z = f(x) m.item_processed() z } (bf) } finally { m

What is Scala's static overloading rule?

戏子无情 提交于 2019-12-11 06:56:03
问题 In many explanations about Scala's implicit precedence, it states that if there is more than one thing with the same precedence, Scala's "static overloading rule" is applied. That rule isn't explained, though. It seems that this expression is used exclusively in this context. What is Scala's static overloading rule? 回答1: This is explained in §6.26.3 of the Scala Language Specification. As also noted in this answer, there is a blog post that lists this resolution in a simpler way: The relative

Adding Overloaded Constructors to Implicit F# Type

我的未来我决定 提交于 2019-12-11 05:08:41
问题 I have created the following type using implicit type construction: open System type Matrix(sourceMatrix:double[,]) = let rows = sourceMatrix.GetUpperBound(0) + 1 let cols = sourceMatrix.GetUpperBound(1) + 1 let matrix = Array2D.zeroCreate<double> rows cols do for i in 0 .. rows - 1 do for j in 0 .. cols - 1 do matrix.[i,j] <- sourceMatrix.[i,j] //Properties ///The number of Rows in this Matrix. member this.Rows = rows ///The number of Columns in this Matrix. member this.Cols = cols //

Can a method argument serve as an implicit parameter to an implicit conversion?

半腔热情 提交于 2019-12-11 04:37:15
问题 The following code in a REPL session: case class Foo(x : Int) case class Bar(x : Int) case class Converter(y : Int) { def convert(x : Int) = x + y } implicit def fooFromBar(b : Bar)(implicit c : Converter) = Foo(c convert (b x)) def roundaboutFoo(x : Int, converter : Converter) : Foo = Bar(x) Gives me this error: error: could not find implicit value for parameter c: Converter def roundaboutFoo(x : Int, converter : Converter) : Foo = Bar(x) In case it's not obvious (implicits), what I'm trying

Scala call-by-name constructor parameter in implicit class

空扰寡人 提交于 2019-12-11 04:09:16
问题 The following code does not compile . Desired is to have a call-by-name constructor parameter in an implicit class as illustrated here, def f(n: Int) = (1 to n) product implicit class RichElapsed[A](val f: => A) extends AnyVal { def elapsed(): (A, Double) = { val start = System.nanoTime() val res = f val end = System.nanoTime() (res, (end-start)/1e6) } } where a call val (res, time) = f(3).elapsed res: Int = 6 time: Double = 123.0 This error is reported in REPL, <console>:1: error: `val'

Generate functions based on type parameter

巧了我就是萌 提交于 2019-12-11 04:06:20
问题 I would like to generate functions for a class accepting 1 type parameter, which wraps a by name value. class C[T](_t: => T) { def t: T = _t } The functions I would like to generate are derived by the functions available on T . What I would like exactly, is to get all the functions available for T , change their contract and implementation in a programmatic way, and make them available for C . By changing their contract, I mean changing their signature so they return C[R] , where R stands for