Questions tagged [scala-implicits]

44 questions
19
votes
3 answers

Best way to handle false unused imports in intellij

Intellij falsely marked some import of Scala implicits as not being use. Is there a way to prevent it from deleting those import when optimized them explicitly for a specific import and not prevent optimized import for the entire project ?
NetanelRabinowitz
  • 1,534
  • 2
  • 14
  • 26
11
votes
3 answers

How can I "pimp my library" with Scala in a future-proof way?

I use Scala implicit classes to extend objects I work with frequently. As an example, I have a method similar to this defined on Spark DataFrame: implicit class DataFrameExtensions(df: DataFrame) { def deduplicate: Boolean = …
Sasgorilla
  • 2,403
  • 2
  • 29
  • 56
11
votes
2 answers

Caching implicit resolution

To reduce compile times of my project, I'm caching certain type classes that are resolved by implicit lookups. This appears somewhat cumbersome though, because the straight forward implementation does not work: scala> implicit val x: String =…
Taig
  • 6,718
  • 4
  • 44
  • 65
8
votes
1 answer

Scala vs Haskell typeclasses: "catchall" instances

The following Haskell type class and instance: class Able a where able :: a -> Int instance Able Int where able x = x is commonly translated to Scala like so: trait Able[A] { def able(a: A): Int } implicit object AbleInt extends Able[Int]…
scravy
  • 11,904
  • 14
  • 72
  • 127
6
votes
2 answers

Defining a variable in scala with two implicits

I came across an interesting post on twitter by scalaLang. Where this code compiles and works class A(implicit implicit val b: Int) val objA = new A()(42) Can someone please explain me how is it working? I read the documentation of implicits but…
Shivansh
  • 3,454
  • 23
  • 46
6
votes
0 answers

Ambiguity in Low priority implicits

I have the following snippet of Scala code: import java.io.PrintWriter trait Write[-A] { def apply(out: PrintWriter)(x: A): Unit } trait LowPriorityWrites { implicit object any extends Write[Any] { override def apply(out:…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
5
votes
1 answer

How to implement Functor[Dataset]

I am struggling on how to create an instance of Functor[Dataset]... the problem is that when you map from A to B the Encoder[B] must be in the implicit scope but I am not sure how to do it. implicit val datasetFunctor: Functor[Dataset] = new…
5
votes
1 answer

Implicit resolution of dependent types in Scala

Consider the following piece of code: trait Foo { type T def value: T } object Foo { def apply[A](v: A): Foo = new Foo { override type T = A override def value = v } } trait Decode[A] { def apply(x: A): String } object Decode { …
pathikrit
  • 32,469
  • 37
  • 142
  • 221
4
votes
2 answers

Ambiguous implicit values for Typeclass

I am trying to abstract out the json parsing logic that gets triggered for a specific type. I started out creating a Parser trait as follows: trait Parser { def parse[T](payload : String) : Try[T] } I have an implementation of this trait called…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
4
votes
1 answer

Chaining implicit conversions of function to a generic class

I have the following code which was supposed to take a function A => Boolean (generic on the input type) and convert it to a generic trait Y[A] through chained implicit conversions: val f: Int => Boolean = ??? trait X[A] { def m1: Unit } implicit…
erdavila
  • 341
  • 2
  • 15
2
votes
2 answers

Strange Scala compiler error when removing a call to a function that has Unit return type, how is this even possible?

Here is a strange situation: If I comment out the call to feed_usingExplicitTypeClassInstance below, then I get a compiler error. Very puzzling. Any explanation ? I mean, I comment out a function call (which returns no value) and then the code does…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
2
votes
0 answers

Ambiguous implicit values when using a generic method with an implicit parameter

In the following code: trait Foo[T] { def get: T } implicit object FooInt extends Foo[Int] { override def get = 0 } implicit object FooString extends Foo[String] { override def get = "0" } def fooImplicitGetter[T](implicit ev: Foo[T]): T =…
Gal
  • 5,338
  • 5
  • 33
  • 55
2
votes
1 answer

Scala Implicit class member is not accessible in object

I am using Scala bCrypt wrapper for encrypting user password, this wrapper provides an implicit class. package object bcrypt { implicit class Password(val pswrd: String) extends AnyVal { def bcrypt: String = B.hashpw(pswrd,…
2
votes
1 answer

Select field by type

I am trying to create a type class that allows to select a field for a given type. This is what I have done so far but the compiler is unable to find the Selector.Aux case class AddressKey(street: String, city: String) trait AddressKeySelector[A]…
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39
2
votes
1 answer

Scala syntax tree returned from inferImplicitValue is failing to evaluate

I am writing a Scala macro (Scala 2.11) where I'd like to obtain the tree representing an implicit variable inside the macro using inferImplicitValue, evaluate that syntax tree, and use the value. I have actually done this, but it doesn't seem to…
eje
  • 945
  • 11
  • 22
1
2 3