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
1
vote
0 answers

Scala: How to avoid copy-pasting the implicit parameters? e.g. typeclass alternative?

This Question is a consequence of the Question How to define an abstract copyable superclass for any case class but one can leave that context alone and focus on this: NOTE: The whole compiling code base (with Scala 2.11.8) can be accessed in github…
SkyWalker
  • 13,729
  • 18
  • 91
  • 187
1
vote
2 answers

How to make calling code agnostic of implicit parameter passed under the hood?

I want to be able to surround a block of code with a transaction. The calling code should be as simple as this: transactional { save("something") } I thought to make the transactional function like this: def transactional(block: => Unit): Unit =…
Jeroen Kransen
  • 1,379
  • 3
  • 19
  • 45
1
vote
0 answers

Create <:<-like implicit in Scala with custom inheritance structure

I have a sealed Entity trait that can be either Root or Descendant[P <: Entity]. So I need a way to check that inheritance in function invocations. The most common Scala way is to require an implicit evidence of some type (A <:< B for plain Scala…
Maxim
  • 1,209
  • 15
  • 28
1
vote
1 answer

Class with its own context bound

The following works: trait Context[T] { def context(x: T): String class Foo[T : Context](x: T) { def bar() = implicitly[Context[T]].context(x) } implicit val c = new Context[Double] { override def context(x: Double): String = "I am a…
avanwieringen
  • 2,404
  • 3
  • 21
  • 28
1
vote
1 answer

How to solve method delegation with scala implicits

How can i solve this simple problem. Class Conversion has a typed method from wich takes two type parameters A and B and returns a B from an A. I have defined some implicits in the companion object to provide default beahviour. My Problem is when i…
Jay
  • 1,035
  • 2
  • 11
  • 22
1
vote
2 answers

How to pass implicit value to a function?

I am new to scala. I am learning implicit variables. How can a pass an implicit variable to a function that calls another function that is gonna use that variable. I know this question seems stupid. Just look at the code that I have written. class…
odbhut.shei.chhele
  • 5,834
  • 16
  • 69
  • 109
1
vote
1 answer

Infinite recursion with Shapeless select[U]

I had a neat idea (well, that's debatable, but let's say I had an idea) for making implicit dependency injection easier in Scala. The problem I have is that if you call any methods which require an implicit dependency, you must also decorate the…
Jeremy
  • 533
  • 3
  • 10
1
vote
2 answers

Implicit resolution with covariance

Why does the following code work when Foo is invariant, but not when it is covariant? The covariant version of Foo produces a type error saying that in the call of useF1, the argument has type Foo[T] but F1 is required. A similar error is produced…
Heatsink
  • 7,721
  • 1
  • 25
  • 36
1
vote
1 answer

Implicit parameters break type inference or inference does not suffice for their resolution?

Type inference works fine in this example until I add the implicit ordering evidence. Type inference rules (from left to right & across parameter lists) seem to be satisfied, but there is something in regards to the implicit that breaks it. case…
lisak
  • 21,611
  • 40
  • 152
  • 243
1
vote
1 answer

Is there any style guidelines about using implicit parameter with default value in scala?

Is it fine to use such solutions like here: def convert[T](x: T)(implicit format = Default) = ... It allows you to not specify implicits if you don't need to. But many libraries (at least Lift Json and Scala Concurrent) avoid such kind of default…
dk14
  • 22,206
  • 4
  • 51
  • 88
1
vote
1 answer

Idiomatic way of branching depending on the existance of a type evidence in Scala

I find myself more than once writing the following ugly pattern: class Something[A, B](implicit ev: A =:= B = null) { ... def doStuff { if (ev == null) ... // know that A is not the same as B else ... // safely assume A is…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
1
vote
2 answers

Implicits over type inference for object transformation

Part of a current project involves converting from types coupled to a database and a generic type used when serializing the results out to clients via Json, the current implementation in Scala uses type inference to correctly perform the…
Sparko
  • 735
  • 6
  • 15
1
vote
0 answers

Class method implicit param cannot be found when not calling from a val

I've got a class that depends on an HList, has a polymorphic function and a method to map this function over the list. The strange thing is that this method can only be invoked on a val, otherwise the compilation fails: import shapeless._ import…
ps_ttf
  • 1,096
  • 7
  • 15
1
vote
0 answers

lift, squeryl record, and scala implicits: adding new method to record, extending AnyVal

I would like to add additional method for Squeryl Record (Lift web framework). The method is named validateTry. See the code below import scala.language.postfixOps import scala.util._ import net.liftweb.record._ package object code { implicit…
Andrey
  • 712
  • 6
  • 16
1
vote
1 answer

scala user defined literals with implicits

I really like Scala for its flexibility and conciseness. With this definition of a money amount: case class MoneyAmount(amount: Double, currency: Currency) trait Currency case object EUR extends Currency case object USD extends Currency You can…
Erik
  • 2,888
  • 2
  • 18
  • 35