implicit Questions
3
The following code does not compile:
import scala.language.implicitConversions
trait Base {
class Wrp[+T](val v: T) // wrapper / internal representation
}
trait BooleanOps extends Base {
// im...
Melan asked 22/1, 2016 at 23:58
3
Solved
I understand implicit parameters and implicit conversions in Scala but I saw this for the first time today: the implicit keyword in front of a parameter in an anonymous function:
Action { implicit...
3
Solved
case class Cat(name: String)
object CuterImplicits {
implicit class CatCuteChecker(c: Cat) {
def isCute(c: Cat) = true
}
}
trait CuteChecker[A] {
def isCute(a: A): Boolean
}
object CheckingF...
2
Solved
I am fairly new to Scala and have been trying to learn and understand implicit conversions and parameters and have encountered a scenario that I find confusing.
For context, I am using Scaldi to d...
1
Solved
When looking at the source of some Scala libraries, e.g. shapeless, I often find traits named LowPriorityImplicits.
Can you please explain this pattern? What is the problem that is solved, and ho...
Mauricio asked 5/11, 2015 at 12:11
1
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 argume...
Wale asked 25/8, 2015 at 18:24
1
I've seen two ways (one less than the other) of declaring implicit for typeclass pattern in Scala.
implicit val instance1 = new Typeclass { def do = ??? }
implicit object instance2 extends Typecl...
1
Solved
Why can't scala find the implicit here?
class A
class Foo {
lazy val x = implicitly[A]
implicit lazy val a = new A
}
error: could not find implicit value for parameter e: A
But this works...
1
Solved
I like implicit definitions. They make the code look nice, they make the user feel some features are naturally available on a class when it's just an implicit definition. Yet, I was thinking about ...
Fango asked 2/10, 2015 at 12:54
1
[error] test.scala:31: ambiguous implicit values:
[error] both method taggedQueryParamDecoder in trait ExternalInstances0 of type [A, T](implicit evidence$2: org.http4s.QueryParamDecoder[A])org.htt...
2
Solved
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 s...
1
Let's say that I have two particular object's from which I retrieve imports. Assume that both objects have multiple, useful imports that I want to use. I'm only including 1 for simplicity of this e...
2
Solved
Given the following object with 2 implicits:
scala> object Foo {
| implicit def stringToInt(x: String) = 555
| implicit def stringToBoolean(x: String) = true
| }
warning: there were two feat...
1
Solved
I have the following code which uses spray-json to deserialise some JSON into a case class, via the parseJson method.
Depending on where the implicit JsonFormat[MyCaseClass] is defined (in-line o...
Brassiere asked 7/9, 2015 at 12:26
1
Solved
If you are like me, you occasionally want to write enhanced methods for Scala collections or sequences, but you'd like to bind the collection type as well as the element type, not just upcast to Se...
Evangelinaevangeline asked 4/9, 2015 at 18:15
1
Solved
Are there rules for implicit type conversion for arguments of the ternary operator?
The ternary operator always needs to return the same type. This type is determined solely by the second and thir...
0
Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence:
sealed trait Digit
case object Zero extends Digit
case object One extends Digit
sealed trait Dense { type N <: Dense...
Chaney asked 23/8, 2015 at 20:7
1
Solved
There is a significant difference in how Scala resolves implicit conversions from "Magnet Pattern" for non-overloaded and overloaded methods.
Suppose there is a trait Apply (a variation of a "Magn...
2
Solved
I was reading through some older Scala posts to better understand type classes, and I ran
across this one that seemed quite useful, but the example seems to have gotten stale.
Can someone help m...
1
Solved
First off, I don't know how to properly label my problem. This might also be the reason why I didn't find helpful resources. Any hints are highly appreciated.
trait Context[T]
{
self =>
trai...
1
I'm using a library which has a class Product like
class Product {
def toString() = "Whatever"
}
I want to override this toString method. So there are two solutions.
1 - Copy the contents o...
2
Solved
I am experimenting with a set of custom container functions and took inspiration from Scala's collections library with regard to the CanBuildFrom[-From, -Elem, -To] implicit parameter.
In Scala's...
5
Solved
I'm designing a class hierarchy, which consists of a base class along with several traits. The base class provides default implementations of several methods, and the traits selectively override ce...
6
Solved
I am trying to create an implicit conversion from any type (say, Int) to a String...
An implicit conversion to String means RichString methods (like reverse) are not available.
implicit def intTo...
1
Solved
I've been playing with the typeclass pattern in Scala, but I haven't been able to figure out how to implement the implicit companion object when the type I'm working with is generic.
For example, ...
Abe asked 29/4, 2015 at 17:16
© 2022 - 2024 — McMap. All rights reserved.