implicit

Scala: Passing one implicit parameter implicitly and the other explicitly. Is it possible?

╄→尐↘猪︶ㄣ 提交于 2020-12-27 08:54:50
问题 Let's consider the function: def foo(implicit a:Int, b:String) = println(a,b) . Now, let us assume that there is an implicit String and Int ( implicit val i1=1 ) in scope but we want to pass an other, not implicit Int ( val i2=2 ) explicitly to foo . How can we do that ? Is it possible? Thanks for reading. 回答1: All I can add is: def foo(implicit a: Int, b: String) = println(a, b) implicit val i1 = 1 implicit val s = "" val i2 = 2 foo(i2, implicitly[String]) 回答2: In case your method has many

Scala: Passing one implicit parameter implicitly and the other explicitly. Is it possible?

て烟熏妆下的殇ゞ 提交于 2020-12-27 08:54:33
问题 Let's consider the function: def foo(implicit a:Int, b:String) = println(a,b) . Now, let us assume that there is an implicit String and Int ( implicit val i1=1 ) in scope but we want to pass an other, not implicit Int ( val i2=2 ) explicitly to foo . How can we do that ? Is it possible? Thanks for reading. 回答1: All I can add is: def foo(implicit a: Int, b: String) = println(a, b) implicit val i1 = 1 implicit val s = "" val i2 = 2 foo(i2, implicitly[String]) 回答2: In case your method has many

Implicit macro. Default implicit value. How?

吃可爱长大的小学妹 提交于 2020-12-15 07:08:32
问题 I don't even know how to ask the question. I have a macro that creates an instance of IsEnum[T] for a type T . I'm doing testing for it, and want to make sure that the implicit is not found for types that are not sealed, or that, in general, don't meet the requirements of an enum. So I created this method for testing def enumOf[T](implicit isEnum:IsEnum[T] = null) = isEnum And then I ensure that enumOf[NotAnEnum] == null But instead, it fails at compile time. One thing is the macro erroring.

Implicit macro. Default implicit value. How?

我与影子孤独终老i 提交于 2020-12-15 07:07:08
问题 I don't even know how to ask the question. I have a macro that creates an instance of IsEnum[T] for a type T . I'm doing testing for it, and want to make sure that the implicit is not found for types that are not sealed, or that, in general, don't meet the requirements of an enum. So I created this method for testing def enumOf[T](implicit isEnum:IsEnum[T] = null) = isEnum And then I ensure that enumOf[NotAnEnum] == null But instead, it fails at compile time. One thing is the macro erroring.

Witness that an abstract type implements a typeclass

人走茶凉 提交于 2020-12-15 05:37:16
问题 I believe my understanding on this is correct but I'd like to check. When creating typeclasses, it feels neater to have them take a single type parameter, like TypeClass[A] . If the typeclass needs to be parameterized in other ways, abstract types can be used, and there is a comparison of the two approaches here: Abstract types versus type parameters So far as I have been able to figure out, one thing which is not mentioned in the link is that if using a type parameter, you can witness that

Witness that an abstract type implements a typeclass

女生的网名这么多〃 提交于 2020-12-15 05:37:11
问题 I believe my understanding on this is correct but I'd like to check. When creating typeclasses, it feels neater to have them take a single type parameter, like TypeClass[A] . If the typeclass needs to be parameterized in other ways, abstract types can be used, and there is a comparison of the two approaches here: Abstract types versus type parameters So far as I have been able to figure out, one thing which is not mentioned in the link is that if using a type parameter, you can witness that

How to make implicits available to inner function

[亡魂溺海] 提交于 2020-12-06 07:07:05
问题 I would like to define implicit value in a wrapper function and make it available to inner function, so far I managed to do that by passing implicit variable from wrapper: case class B() trait Helper { def withImplicit[A]()(block: => A): A = { implicit val b: B = B() block } } class Test extends Helper { def useImplicit()(implicit b: B): Unit = {...} def test = { withImplicit() { implicit b: B => useImplicit() } } } Is it possible to avoid implicit b: B => and make implicit val b: B = B()

Enforcing that dependent return type must implement typeclass

吃可爱长大的小学妹 提交于 2020-11-29 10:15:13
问题 I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec typeclass below, they must also ensure that the return value of the getElem method implements another typeclass ( IsVecElem ). My attempts to make this work look something like this: // A typeclass for an vector element abstract class IsVecElem[A, T: Numeric] { def dataOnly(self: A): T } // A typeclass for a vector abstract class IsVec[A, T:

Enforcing that dependent return type must implement typeclass

巧了我就是萌 提交于 2020-11-29 10:13:43
问题 I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec typeclass below, they must also ensure that the return value of the getElem method implements another typeclass ( IsVecElem ). My attempts to make this work look something like this: // A typeclass for an vector element abstract class IsVecElem[A, T: Numeric] { def dataOnly(self: A): T } // A typeclass for a vector abstract class IsVec[A, T: