Questions tagged [scala-implicits]
44 questions
2
votes
0 answers
Avoiding implicit parameters becoming implicit conversions
The intended output of the following code is the digit, 0.
object Bad extends App {
implicit val massTable: Map[String, Int] =
Map("H" -> 1, "He" -> 4, "O" -> 16)
Implementation.doWork()
}
object Implementation {
def doWork()(implicit…

vossad01
- 11,552
- 8
- 56
- 109
2
votes
1 answer
Generic String interpolator using StringContext
I'm trying to create some simple custom String interpolator, and I'm successful as long as I don't try to use a type parameter.
import scala.concurrent.Future
object StringImplicits {
implicit class FailureStringContext (val sc : StringContext)…

sscarduzio
- 5,938
- 5
- 42
- 54
2
votes
1 answer
Scala: enforcing precedence of an implicit declared in a trait
There are countless questions on this topic and a quite good, if not very precisely worded article here: Revisiting implicits without the import tax. I've read most of them, but have a problem with overriding default CanBuildFrom implicits without…

Turin
- 2,208
- 15
- 23
1
vote
2 answers
Why are functions in an implicit class not available?
I am trying to teach myself Scala and am using IntelliJ IDEA as my IDE. I have launched IntelliJ's sbt shell, run console and then entered the following:
import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, SparkSession}
import…

jamiet
- 10,501
- 14
- 80
- 159
1
vote
0 answers
How to find implicit function(s) using Scala metaprogramming?
In scala language, the implicit scope of a class is erased in runtime, e.g. if defining:
case class A(i: Int)
object A {
implicit def toInt(a: A) = a.i
}
Then for an array:
val arr = Array[Any](A(1), 2)
it is impossible to write:
arr.map(_ +…

tribbloid
- 4,026
- 14
- 64
- 103
1
vote
1 answer
Get an implicit instance by class name
What I'm trying to do is: get an implicit instance from the class name.
The main problem that I can't get an implicit instance for a class type that created at runtime.
What I have:
trait Base
case class A() extends Base
case class B() extends…

Nikolay Khoroshevskyi
- 13
- 1
- 4
1
vote
1 answer
Can not find implicit conversion for a parameter of method with lower type bound
I have this code:
class Action[T]
class Insert[T] extends Action[T]
case class Quoted[T]()
implicit def unquote[T](q: Quoted[T]): T = {
throw new Exception("Success")
}
def test[A <: Action[_]](a: A): A = {
return a
}
try {
…

mixel
- 25,177
- 13
- 126
- 165
1
vote
1 answer
Joda Marshalling/Unmarshalling in scala
I'm trying to use akka.http.scaladsl.testkit.responseAs in order to test some endpoints, but I can't figure out how to handle the marshalling/unmarshalling process of a org.joda.time.DateTime object. For instance, consider the case class below:
case…

goiaba
- 11
- 1
1
vote
1 answer
Scala unexpectedly compiles a file, are there implicit conversions at work?
I am trying to figure out why the classes below compile without error in Scala.
I would expect the createTestClass method in the DoTest class to fail with a typing error because it tries to supply the implicit parameter which has type TypeClass[A],…

Ozymandias
- 521
- 2
- 16
1
vote
2 answers
Why am I getting "could not find implicit value for parameter messages: play.api.i18n.Messages"
I am getting the above compilcation error on Messages("title")
import play.api.i18n.Messages
import play.api.mvc._
import scala.concurrent.Future
trait ApplicationController extends Controller {
def get = Action.async {
implicit request =>…

Ramin
- 267
- 1
- 4
- 13
1
vote
1 answer
Changing implicit variable from private to public cause 'covariant type T occurs in invariant position' Error
One of my class has a TypeTag:
abstract class Tagged[+T](implicit ttg: TypeTag[T])
the TypeTag (in this case a private variable) has caused some problem as its not serializable. So try to declare it as @transient:
abstract class…

tribbloid
- 4,026
- 14
- 64
- 103
1
vote
1 answer
Generically adding implicits to both TreeSet and TreeMaps
I want to add some helpful implicits to both mutable and immutable TreeMaps and TreeSets in Scala.
Here is my attempt:
First try to define the least upper bound of TreeMap and TreeSet that has headOption/lastOption (from GenTraversableLike) and…

pathikrit
- 32,469
- 37
- 142
- 221
1
vote
2 answers
scala implicit in function type definition
I have following abstract class:
abstract class FieldProvider[+T: Writes](db: DB)(implicit i: RequestAction, j: ExecutionContext) {}
and following implementations:
class LengthProvider extends FieldProvider ...
object LengthProvider extends ((DB)…

roman-roman
- 2,746
- 19
- 27
1
vote
1 answer
scala typing require implicit
I'm trying to build following
I have a parent generic class
abstract class ResultProvider[+T: Writes](db: DB) {
def get(id: Long): Future[Seq[T]]
}
And some implementations, e.g.
class LengthProvider(db: DB) extends…

roman-roman
- 2,746
- 19
- 27
1
vote
1 answer
Generically extending Scala collections
I have a Seq[(A, B)]. I wanted to add an implicit method to such collections so I can do .toMultiMap to get back a Map[A, Seq[B]].
This was my first attempt:
implicit class PairsExtensions[A, B](t: Traversable[(A, B)]) {
def toMultiMap: Map[A,…

pathikrit
- 32,469
- 37
- 142
- 221