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
2
votes
2 answers

Excluding type evidence parameters from analysis in Scala when using -Ywarn-unused

Compiling a program that contains a type evidence parameter in Scala (such as T <:< U) can cause a warning when -Ywarn-unused is passed to the compiler. Especially in the case when the type evidence parameter is used to verify a constraint encoded…
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

Implicit conversion in monadic for comprehension in Scala

Say I have the following functions: case class ErrA(msg: String) case class ErrB(msg: String) def doA(): Either[ErrA, Int] = Right(2) def doB(): Either[ErrB, Int] = Right(3) ErrA and ErrB are unrelated types and are not actually declared in the…
2
votes
1 answer

Bidirectional implicit conversion in Scala

Consider the follwing generic function: def compare[I:Ordering,T:Ordering](i:I,t:T):Int It should compare a value of type I with a value of type T with both of them assumed to have Ordering defined. The comparison should work if there is either a…
Krle
  • 70
  • 7
2
votes
1 answer

Avoiding explicit delegation when using implicits to implement a Trait

I am using several classes like this: class MyClassOne { doSomething(a : A) : B ... } class MyClassTwo { doSomething(a : A) : B ... } The source for the classes is not under my control. Although the classes have methods with the same…
mushroom
  • 6,201
  • 5
  • 36
  • 63
2
votes
1 answer

Scala implicit not found "not enough arguments"

In this simple code i have a method buildResponse which takes a type parameter T which must be a subtype of Response. Now the compiler tells me it does not find an implicit value when i call new ResponseBuilder().build\[T]() inside alltough i have…
Jay
  • 1,035
  • 2
  • 11
  • 22
2
votes
0 answers

Implicit Macro resolution

I'm trying to get an implicit parameter to be generated by a macro. When requesting the StructTypeInfo implicit, there is a compiler error, and log-implicits shows: [info] Test.scala:29: materializeCaseClassSchemaType is not a valid implicit value…
Dan Osipov
  • 1,429
  • 12
  • 15
2
votes
1 answer

Consuming implicit from consuming class

I'm looking for the best syntax for having contained classes pick up the implicits exposed by their containing classes? The scenario I have has two dependencies abstracted for testing: The creation of the application's actorsystem, and a webclient…
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
2
votes
2 answers

Is it possible to have implicit Ordering[Option[T] and Ordered[Option[T]] at the same time in Scala?

My code: import Ordered.orderingToOrdered import java.util.Date val (d1, d2) = (Option(new Date()), Option(new Date())) d1 compare d2 result with -Xlog-implicits: Information:(268, 5) math.this.Ordering.Option is not a valid implicit value for …
Turin
  • 2,208
  • 15
  • 23
2
votes
1 answer

What's the advantage of a Scala "context bound" over a normal parameter?

I'm reading about the context bounds and implicit parameters that are supposed to work like type classes. The examples I see often use Ordering[T]. Something like: foo[T : Ordering](a: T, b: T) which is sugar for foo[T](a: T, b: T)(implicit ord:…
Rob N
  • 15,024
  • 17
  • 92
  • 165
2
votes
1 answer

Implicit rule in makefile

Consider the following makefile: default: foo.o clean: rm -f fmake test_second %.o: %.c echo This is customized implicit rule %.o: %.c echo This is overriden implicit rule The output of the make command is echo This is overriden…
user2889159
2
votes
2 answers

Defining An Implicit Conversion For A Recursive And Nested Type Structure

I've defined the following class, PositionSpace, which has nested classes Position and SubPositionSpace. SubPositionSpace is itself both a Position and a PositionSpace (so it is a position that also has its own positions defined). I've defined a…
Nimrand
  • 1,748
  • 2
  • 16
  • 29
2
votes
2 answers

Can I tell scala how to prefer more specific implicit, rather than give "ambiguous implicit" error?

The following code gives an error. class Base { } class Sub extends Base { } class Writer[-T] { def write(t: T) { } } object AmbImplicits { implicit object BaseWriter extends Writer[Base] implicit object SubWriter extends Writer[Sub] def…
Rob N
  • 15,024
  • 17
  • 92
  • 165
2
votes
0 answers

Trying to use sortBy to sort a list of tuples

In my scala eclipse worksheet I do: val values: List[(Char, Int)] = List(('z', 7), ('b', 6)) //> values : List[(Char, Int)] = List((z,7), (b,6)) val sortedValues = values.sortBy(_._1) //>…
More Than Five
  • 9,959
  • 21
  • 77
  • 127
2
votes
1 answer

How to getClass implicitly

I'm writing a Scala wrapper over some Java code with a method signature like method(cl: Class[_], name: String) and many getClass methods in code looks not good: Creator.create(getClass, "One") Creator.create(getClass,…
4lex1v
  • 21,367
  • 6
  • 52
  • 86