Questions tagged [scala-implicits]
44 questions
1
vote
2 answers
Scala Can't Find Implicit and I Don't Know Why - Can Someone Point Me in the Right Direction?
I'm trying to write a factory to create case classes. I'm creating a command line tool that accepts String representations for the parameters of the case class. Anyway the idea being to limit changes for additions to creating an implicit for the new…

bbarrington
- 115
- 7
1
vote
1 answer
Scala: recursive implicit type
I have the following traits for parsing that give file positions for the beginning and end of the object:
case class FilePosn(lineNum :Int, tabs: Int, spaces: Int, fileName: String)
{/*code omitted*/}
trait PosnEnds
{
def startPosn: FilePosn
…

Rich Oliver
- 6,001
- 4
- 34
- 57
0
votes
1 answer
Implicit conversions in generic function
I need to convert from Any to basic numeric types like Int or Double. I implemented these conversions by using Scala implicits. My code is similar to this one:
def convertAny[T](any: Any)(implicit run: Any => Option[T]) = run.apply(any)
implicit…

Álvaro Valencia
- 1,187
- 8
- 17
0
votes
1 answer
Scala type parameter inference based on return type (function vs method)
I try to create a small matching library in Scala.
I have the following type representing a matcher that reprsents a constraint on a type T:
trait Matcher[-T] extends (T => Boolean)
and a matches function that checks whether that constraint holds…

fikovnik
- 3,473
- 3
- 28
- 29
0
votes
2 answers
Play not finding implicit definition
I am writing a JSON Writes. In models/Users.scala, I have defined an implicit object with implicit definitions.
object UserImplicits {
/*Writes (write to JsValue) are used by toJson method of Json object to convert data (say the model) to…

Manu Chadha
- 15,555
- 19
- 91
- 184
0
votes
2 answers
implicit function that compares two closed classes in Scala
I am working with a Location object from another closed API and it already has a toString() method that returns a String. I just want an implicit function that can compare two Location instances by comparing their toString() values. So I will be…

uh_big_mike_boi
- 3,350
- 4
- 33
- 64
0
votes
0 answers
In scala, why this implicit parameter cannot be inferred from the same scope?
I'm writing a simple Apache Spark utility that automatically creates an AccumulatorV2 based on provided initial value:
import java.lang
type Acc[T] = AccumulatorV2[T, T]
implicit val long1: Long => Acc[lang.Long] = _ => new…

tribbloid
- 4,026
- 14
- 64
- 103
0
votes
2 answers
Scala implicit conversion for object
Suppose that I have the following code snippet:
import scala.language.implicitConversions
sealed trait Command {
val typeName: String
}
object Command {
implicit def command2String(implicit c: Command): String =
c.typeName
}
case object…

Andrii Abramov
- 10,019
- 9
- 74
- 96
0
votes
2 answers
Understanding the Scala Map object when mapping to the head of a list
Hi I have the following data and want to map it to the first item in the second parameter. So for:
1 -> List((1,11))
1 -> List((1,1), (1,111))
I want:
(1,11)
(1,1)
When this data is in an RDD I can do the following:
scala> val m =…

Breandán
- 1,855
- 22
- 34
0
votes
3 answers
String companion object in scala
Given a type which has a "converter", I would like to have automatic conversion on method call using this type's companion object. That is, given the following definition,
case class Converted(name: String)
trait Converter[A] {
def perform:…

Mikaël Mayer
- 10,425
- 6
- 64
- 101
0
votes
1 answer
Scala - Is it possible to define an implicit function in super class?
I'm writing a web service using Scala Play. The functionality is ok but I'm refactoring some parts to make my code more readable and clean.
I've used implicit for each of my entity classes to make them convertable to Json. I've also injected toJson…

Bardia Heydari
- 777
- 9
- 24
0
votes
2 answers
Scala: get outer class from inner class in constructor
I have an anonymous inner class, and I want to acces the (anonymous) outer class of it in the constructor. So I want to implement this method:
new Outer {
new Inner {
}
}
class Outer {
}
class Inner {
def outerClass: Outer = ???
}

Bert Becker
- 41
- 5
0
votes
3 answers
Scala not letting me add integers
Here's some code which looks reasonable enough to me:
val myMap: Map[Int, Int] = ((x: Int) => Map[Int, Int](1 -> x + 1, 2 -> x + 2))(4)
When I try to compile it, I get two errors like this:
Error:(20, 68) type mismatch;
found : Int(1)
required:…

Buck Shlegeris
- 517
- 6
- 22
-3
votes
1 answer
How does implicit types in scala work with reference to this https://youtu.be/hC4gGCD3vlY?t=263
How does implicit types in scala work with reference to this https://youtu.be/hC4gGCD3vlY?t=263.
Also I did not understand why he mentions that the convertAtoB object is static.

hemshankar sahu
- 65
- 1
- 7