scala

Why is scalatest MockitoSugar deprecated?

不问归期 提交于 2021-01-02 14:00:11
问题 I'm new to writing junit tests in Scala and I'm using Mockito to mock objects. I'm also using scalatest_2.12-3.0.4 . The ScalaTest documentation (like here) shows the syntax to create the mock using MockitoSugar, i.e. val mockCollaborator = mock[Collaborator] Eclipse shows org.scalatest.mock.MockitoSugar crossed out in the import statement, indicating it is deprecated. The only alternative I've found is, don't use MockitoSugar and instead do: val mockCollaborator = mock(classOf[Collaborator])

Simplifying Option[Boolean] expression in Scala

微笑、不失礼 提交于 2021-01-02 07:52:28
问题 I have code like that: optionBoolean.getOrElse(false) && otherOptionBoolean.getOrElse(false) And Scalastyle tells me that it can be simplified. How? 回答1: You can try the following: Seq(optionBoolean, otherOptionBoolean).forall(_.contains(true)) In Scala 2.13 (it is very similar in prior versions) the forall method is located at IterableOnce, and its implementation is: def forall(p: A => Boolean): Boolean = { var res = true val it = iterator while (res && it.hasNext) res = p(it.next()) res }

Play: How to remove the fields without value from JSON and create a new JSON with them

僤鯓⒐⒋嵵緔 提交于 2021-01-02 06:37:08
问题 Given the following JSON: { "field1": "value1", "field2": "", "field3": "value3", "field4": "" } How do I get two distinct JSONs, one containing the fields with value and another one containing the fields without value? Here below is how the final result should look like: { "field1": "value1", "field3": "value3" } { "field2": "", "field4": "" } 回答1: You have access to the JSON object's fields as a sequence of (String, JsValue) pairs and you can filter through them. You can filter out the ones

What's the difference between LazyList and Stream in Scala?

我的梦境 提交于 2021-01-02 06:11:07
问题 I noticed that Stream is deprecated in Scala 2.13 and they suggest using LazyList . They also say "Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)". What does it exactly mean ? Why did they deprecate Stream ? 回答1: NthPortal, a contributor to LazyList , states in Update and improve LazyList docs #7842 The key difference between LazyList and Stream - and its key feature - is that whether or not it is lazy is evaluated lazily. I'm not sure how best to convey

What's the difference between LazyList and Stream in Scala?

孤街浪徒 提交于 2021-01-02 06:08:26
问题 I noticed that Stream is deprecated in Scala 2.13 and they suggest using LazyList . They also say "Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)". What does it exactly mean ? Why did they deprecate Stream ? 回答1: NthPortal, a contributor to LazyList , states in Update and improve LazyList docs #7842 The key difference between LazyList and Stream - and its key feature - is that whether or not it is lazy is evaluated lazily. I'm not sure how best to convey

Static methods in interface require -target:jvm-1.8

白昼怎懂夜的黑 提交于 2021-01-02 05:29:45
问题 I'm building scala project using gradle 4.5, scala 2.11.11/2.12.4 with JDK 1.8.0_162 and it was working fine until I upgrade to scala 2.11.12. With 2.11.12 I keep getting compile error Static methods in interface require -target:jvm-1.8 I've been trying to search in google and add couple of stuffs like ScalaCompileOptions.metaClass.useAnt = false Or targetCompatibility="1.8" but none of them fix the issue. 回答1: Ok, after struggling with this problem for a few weeks and decided to post on SO,

Static methods in interface require -target:jvm-1.8

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-02 05:27:48
问题 I'm building scala project using gradle 4.5, scala 2.11.11/2.12.4 with JDK 1.8.0_162 and it was working fine until I upgrade to scala 2.11.12. With 2.11.12 I keep getting compile error Static methods in interface require -target:jvm-1.8 I've been trying to search in google and add couple of stuffs like ScalaCompileOptions.metaClass.useAnt = false Or targetCompatibility="1.8" but none of them fix the issue. 回答1: Ok, after struggling with this problem for a few weeks and decided to post on SO,

sbt: set specific scalacOptions options when compiling tests

不问归期 提交于 2021-01-02 05:04:50
问题 Normally I use this set of options for compiling Scala code: scalacOptions ++= Seq( "-deprecation", "-encoding", "UTF-8", "-feature", "-unchecked", "-language:higherKinds", "-language:implicitConversions", "-Xfatal-warnings", "-Xlint", "-Yinline-warnings", "-Yno-adapted-args", "-Ywarn-dead-code", "-Ywarn-numeric-widen", "-Ywarn-value-discard", "-Xfuture", "-Ywarn-unused-import" ) But some of them don't play well with ScalaTest, so I would like to disable -Ywarn-dead-code and -Ywarn-value

sbt: set specific scalacOptions options when compiling tests

我是研究僧i 提交于 2021-01-02 05:03:28
问题 Normally I use this set of options for compiling Scala code: scalacOptions ++= Seq( "-deprecation", "-encoding", "UTF-8", "-feature", "-unchecked", "-language:higherKinds", "-language:implicitConversions", "-Xfatal-warnings", "-Xlint", "-Yinline-warnings", "-Yno-adapted-args", "-Ywarn-dead-code", "-Ywarn-numeric-widen", "-Ywarn-value-discard", "-Xfuture", "-Ywarn-unused-import" ) But some of them don't play well with ScalaTest, so I would like to disable -Ywarn-dead-code and -Ywarn-value

Clarifying contravariance nature of the return type of a function as parameter a function of an outer convariant container

﹥>﹥吖頭↗ 提交于 2021-01-01 09:40:48
问题 In Option we have def getOrElse[B >: A](default: => B): B = this match { case None => default case Some(a) => a } def orElse[B >: A](obj: => Option[B]): Option[B] = this match { case None => obj case _ => this } In Either we have: def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] I understand what is going and why, a rather extended example could be this OrElse( { Option[B]}).map{....} If B is such that A :> B, then if Some(a) you get Some(a).map(f:B => ???) then Kaboom generally