implicit

How to stub a method call with an implicit matcher in Mockito and Scala

送分小仙女□ 提交于 2021-02-20 05:52:51
问题 My application code uses AService trait AService { def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response] } to register a new user. Class Tenant is a simple case class: case class Tenant(val vstNumber:String, val divisionNumber:String) Trait AServiceMock mimics the registration logic by using a mocked version of AService trait AServiceMock { def registrationService = { val service = mock[AService] service.registerNewUser(anyString) returns Future(fixedResponse)

How to stub a method call with an implicit matcher in Mockito and Scala

℡╲_俬逩灬. 提交于 2021-02-20 05:52:03
问题 My application code uses AService trait AService { def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response] } to register a new user. Class Tenant is a simple case class: case class Tenant(val vstNumber:String, val divisionNumber:String) Trait AServiceMock mimics the registration logic by using a mocked version of AService trait AServiceMock { def registrationService = { val service = mock[AService] service.registerNewUser(anyString) returns Future(fixedResponse)

How to set type parameter bound in scala to make generic function for numerics?

非 Y 不嫁゛ 提交于 2021-02-19 07:12:58
问题 I want to make a sum function that works with all Numeric types. This works: object session { def mapReduce[A](f: A => A, combine: (A, A) => A, zero: A, inc: A) (a: A,b: A) (implicit num:Numeric[A]): A = { def loop(acc: A, a: A) = if (num.gt(a, b)) acc else combine(f(a), mapReduce(f, combine, zero, inc)(num.plus(a, inc), b)) loop(zero, a) } def sum(f: Int => Int) (a: Int, b: Int) : Int = { mapReduce(f, (x: Int, y: Int) => x + y, 0, 1)(a, b)} sum(x => x)(3, 4) //> res0: Int = 7 def product(f:

When doing implicit resolution with type parameters, why does val placement matter?

家住魔仙堡 提交于 2021-02-15 05:33:51
问题 In one file, I have: trait JsonSchema[T] { val propertyType: String override def toString: String = propertyType } object JsonSchema { implicit def stringSchema: JsonSchema[String] = new JsonSchema[String] { override val propertyType: String = "string" } implicit def intSchema: JsonSchema[Int] = new JsonSchema[Int] { override val propertyType: String = "integer" } implicit def booleanSchema: JsonSchema[Boolean] = new JsonSchema[Boolean] { override val propertyType: String = "boolean" } } In

Scala Kleisli throws an error in IntelliJ

人盡茶涼 提交于 2021-02-13 12:27:55
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>

Scala Kleisli throws an error in IntelliJ

為{幸葍}努か 提交于 2021-02-13 12:23:00
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>

Scala Kleisli throws an error in IntelliJ

邮差的信 提交于 2021-02-13 12:22:54
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>

Scala Kleisli throws an error in IntelliJ

我的未来我决定 提交于 2021-02-13 12:21:02
问题 trying to implement Kleisli category for a made-up Partial type in Scala (reading Bartosz Milewski's "category theory for programmers", that's exersize for chapter 4) object Kleisli { type Partial[A, B] = A => Option[B] implicit class KleisliOps[A, B](f1: Partial[A, B]) { def >=>[C](f2: Partial[B, C]): Partial[A, C] = (x: A) => for { y <- f1(x) z <- f2(y) } yield z def identity(f: Partial[A, B]): Partial[A, B] = x => f(x) } val safeRecip: Partial[Double, Double] = { case 0d => None case x =>

How to handle Option with an encoder typeclass in scala

人盡茶涼 提交于 2021-02-10 22:48:07
问题 I have a classic Encoder typeclass. trait Encoder[A] { def encode(a: A): String } I have two questions Question 1 : where does the divergence comes from : [error] … diverging implicit expansion for type sbopt.Test.Encoder[None.type] [error] starting with value stringEncoder in object Test [error] show(None) implicit val stringEncoder = new Encoder[String] { override def encode(a: String): String = a } implicit def optionEncoder[A: Encoder]: Encoder[Option[A]] = (a: Option[A]) => { val

HList foldLeft with tuple as zero

僤鯓⒐⒋嵵緔 提交于 2021-02-10 15:46:48
问题 I'm trying to foldLeft on a HList with an accumulator of type (HL, Int) , where HL is a HList. The program below does not compile. However, if I switch to a simpler accumulator of type HL (by just switching the commented lines with the ones above), it compiles and it works. Wrapping an HList in a tuple breaks the implicit resolution for the leftFolder. What am I missing? package foo.bar import shapeless.{:+:, ::, CNil, Coproduct, Generic, HList, HNil, Lazy, Poly2} import shapeless.ops.hlist.