Questions tagged [implicits]

Anything related to Scala implicit parameters or conversions

Implicits parameters and conversions are a feature of the Scala language. An implicit parameter is one that can be automatically inferred based on its type and the values in scope, without you needing to pass in the value of the argument explicitly. An implicit conversion function converts one type to another automatically on-demand, without needing to call the function explicitly.

170 questions
13
votes
5 answers

Scala view application puzzler

Say we have the following two traits: trait Foo[A] { def howMany(xs: List[A]) = xs.size } trait Bar And an implicit conversion from the second to the first: implicit def bar2foo[A](bar: Bar) = new Foo[A] {} We create a Bar and a list of…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
12
votes
3 answers

Scala named and default arguments in conjunction with implicit parameters

Consider the following: def f(implicit a: String, y: Int = 0) = a + ": " + y implicit val s = "size" println(f(y = 2)) The last expression causes the following error: not enough arguments for method f: (implicit a: String, implicit…
Martin Studer
  • 2,213
  • 1
  • 18
  • 23
12
votes
1 answer

Distinction between type aliases and type lambdas

This question is about a limitation of Scala's implicit resolution system that I've run into a few times when using Scalaz and that doesn't make a lot of sense to me. I've distilled the problem to a Scalaz-less version below, but I'm happy to…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
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
1 answer

Scala - Co/Contra-Variance as applied to implicit parameter selection

I've got a trait like this: trait CanFold[-T, R] { def sum(acc: R, elem: T): R def zero: R } With a function that works with it like this: def sum[A, B](list: Traversable[A])(implicit adder: CanFold[A, B]): B = …
Alexandru Nedelcu
  • 8,061
  • 2
  • 34
  • 39
10
votes
1 answer

Context bounds with two generic parameters

In Scala, I can use context bounds: def sort[T : Ordered](t: Seq[T]) To mean the same thing as: def sort[T](t: Seq[T])(implicit def Ordered[T]) What if I have a class with two generic parameters. I.e. I want to be able to ensure that I have a…
schmmd
  • 18,650
  • 16
  • 58
  • 102
9
votes
6 answers

How to add a factory method to an existing Java class in Scala

In a pure Scala environment, I could do the following if I wanted to "add" a factory method to an existing object: object Test object Extensions { object RichTest { def someFactory = new Test() } implicit def fromTest(t:…
Leo
  • 37,640
  • 8
  • 75
  • 100
9
votes
2 answers

Scala: How can an import prevent finding an implicit value?

I could use suggestions debugging an implicit: I want to use the implicit, x: type T trait HasT { implicit def x: T = ... } But I also need a wildcard import from some package foo. I've tried two different ways of introducing both: class UseT…
stewSquared
  • 827
  • 5
  • 24
9
votes
1 answer

Is there any advantage to using Mapper vs Implicit Operators?

Mapper Automap: Mapper.CreateMap() .ForMember(o1 => o1.PropName, mapper => mapper.MapFrom(o2 => o2.Prop2Name)); Mapper.Map(object1, object2); Implicit operator: public static implicit operator Object1(Object2 o2) { …
Boanerge
  • 369
  • 2
  • 15
8
votes
4 answers

ambigious implicits

The question is why doesn't the following code work with type inference (below is a REPL session to demonstrate), and can it be fixed? More specifically, how is this different from the use of CanBuildFrom which the compiler uses to infer the return…
IttayD
  • 28,271
  • 28
  • 124
  • 178
8
votes
2 answers

Function implicit parameters not any more so after passing it to a higher order function

In Scala you can do things like: def foo(implicit v: Int) = println(v); def h(x: Int) = { implicit val i: Int = x; foo } h(42) > 42 h call gets foo reference as a closure. It wouldn't be strange to try passing foo to h as a parameter: def g(x:…
7
votes
1 answer

Inferring type of generic implicit parameter from return type

Say I have a simple class like this abstract class Foo { implicit val impInt: Int = 42 def f[A]()(implicit a: A): A val f2: Int = f() } When declaring val f2, compiler is able to infer that the type of implicit parameter of function f is Int…
slouc
  • 9,508
  • 3
  • 16
  • 41
7
votes
1 answer

Why does Scala implicit resolution fail for overloaded method with type parameter?

The first example successfully finds the implicit conversion to the method foo(String), however as soon as I add a type parameter (see fails) the compiles doesn't resolve it anymore: object works { class A { def foo(): String = ??? } …
Michael Pollmeier
  • 1,370
  • 11
  • 20
7
votes
1 answer

Scala implicit conversion is applying under some conditions but not others

Here is a simple reproducer, where I define a "commutative" pair type that comes with an implicit reordering conversion. The implicit conversion is applied by the compiler as expected if the argument to function f is in a pre-existing named value…
eje
  • 945
  • 11
  • 22
7
votes
1 answer

scala implicit causes StackOverflowError

How does this implicit val cause a StackOverFlowError? (pared down my original code, to still cause the error) object Complicit { // a class with name, default, and conversion function as implicit val case class CC[A](name: String, defaultValue:…
Core
  • 410
  • 2
  • 10
1
2
3
11 12