scala

Finding the second matching implicit

☆樱花仙子☆ 提交于 2021-02-10 13:25:40
问题 Consider the following setup: trait Foo[A] object Foo extends Priority2 trait Priority0 { implicit def foo1: Foo[Int] = new Foo[Int] {} } trait Priority1 extends Priority0 { implicit def foo2: Foo[Boolean] = new Foo[Boolean] {} } trait Priority2 extends Priority1 { implicit def foo3: Foo[Double] = new Foo[Double] {} } Now, in a REPL (having loaded the above code up), I can do the following: scala> def implicitlyFoo[A](implicit foo: Foo[A]) = foo implicitlyFoo: [A](implicit foo: Foo[A])Foo[A]

what should a scala implicit macro have to return to tell the compiler “forget my result, continue your search”

不羁的心 提交于 2021-02-10 12:42:39
问题 I have an implicit macro with a greedy signature implicit def materializeHelper[C <: Any]: Helper[C] = macro materializeHelperImpl[C] def materializeHelperImpl[C <: Any: ctx.WeakTypeTag](ctx: blackbox.Context): ctx.Expr[Helper[C]] = ??? According to it's signature it would materialize a Helper[C] for any C. But the body is much more picky. It only accepts C s which are sealed traits. What should the macro return to tell the compiler "forget my result, continue your implicit search as if I

Differences between case object T and case class T() when defining ADT?

穿精又带淫゛_ 提交于 2021-02-10 12:22:08
问题 Let's say in scala I have an ADT as follows: sealed trait Animal object Animal { case class Lion(name: String) extends Animal case class Elephant(name:String) extends Animal case object Tiger extends Animal } Here, is it preferable to declare Tiger as a case object or should it be declared as an empty case class i.e case class Tiger() ? Does one have any advantage over other? 回答1: If there is the only Tiger it should be an object. If there can be several equal Tiger s it should be a class.

type mismatch; found : Long required: Int

人盡茶涼 提交于 2021-02-10 09:25:51
问题 I have a method that is supposed to return an Long. But I get an error : **type mismatch; found : Long required: Int** Here is the method: def getRandom_IMEI(from : Long,to : Long) : Long = { if (from < to) return from + new scala.util.Random().nextInt(Math.abs(to - from)); return from - new scala.util.Random().nextInt(Math.abs(to - from)); } and when I make a call to this method like this : def IMEI() : String ={ var str: String ="" var rand:Long = 0 rand = functest.getRandom_IMEI

type mismatch; found : Long required: Int

♀尐吖头ヾ 提交于 2021-02-10 09:25:07
问题 I have a method that is supposed to return an Long. But I get an error : **type mismatch; found : Long required: Int** Here is the method: def getRandom_IMEI(from : Long,to : Long) : Long = { if (from < to) return from + new scala.util.Random().nextInt(Math.abs(to - from)); return from - new scala.util.Random().nextInt(Math.abs(to - from)); } and when I make a call to this method like this : def IMEI() : String ={ var str: String ="" var rand:Long = 0 rand = functest.getRandom_IMEI

Scala solution to nQueen using for-comprehension

天涯浪子 提交于 2021-02-10 09:21:19
问题 I have some difficulty in understanding the Scala solution to the n Queens problem, below is the implementation assuming isSafe is defined correctly def queens(n: Int): Set[List[Int]] = { def placeQueens(k: Int): Set[List[Int]] = k match { case 0 => Set(List()) case _ => for { queens <- placeQueens(k - 1) col <- 0 until n if isSafe(col, queens ) }yield k :: queens } placeQueens(n) } The for comprehension, as I have seen, theoretically should return a buffered collection, and I see here it

Dependency Injection to Play Framework 2.5 modules

試著忘記壹切 提交于 2021-02-10 07:58:05
问题 I have a module class with the following signature: class SilhouetteModule extends AbstractModule with ScalaModule { I would like to inject configuration: class SilhouetteModule @Inject() (configuration: Configuration) extends AbstractModule with ScalaModule { But it fails with the following error. No valid constructors Module [modules.SilhouetteModule] cannot be instantiated. The Play documentation mentions that In most cases, if you need to access Configuration when you create a component,

How i can migrate my getPrime() method so it only returns me prime number but not ()

岁酱吖の 提交于 2021-02-10 06:33:12
问题 Problem : I just want to call getPrime(100) and return only the prime numbers and I don't want use collect . def main(args: Array[String]){ getPrime(100).collect{ case i:Int => println(i); } } Here I want to change something def getPrime(range : Int) = { Range(2,range).map(x => if(isPrime(x)) x); } def isPrime(no : Int) = !Range(2,Math.sqrt(no).toInt + 1).exists(x => no%x ==0) 回答1: As has already been pointed out, you don't want to use if without an else , and you shouldn't use map() when

How i can migrate my getPrime() method so it only returns me prime number but not ()

房东的猫 提交于 2021-02-10 06:32:29
问题 Problem : I just want to call getPrime(100) and return only the prime numbers and I don't want use collect . def main(args: Array[String]){ getPrime(100).collect{ case i:Int => println(i); } } Here I want to change something def getPrime(range : Int) = { Range(2,range).map(x => if(isPrime(x)) x); } def isPrime(no : Int) = !Range(2,Math.sqrt(no).toInt + 1).exists(x => no%x ==0) 回答1: As has already been pointed out, you don't want to use if without an else , and you shouldn't use map() when

How does type class resolution in scala work?

对着背影说爱祢 提交于 2021-02-10 06:24:11
问题 I have a function with a type parameter and I want to find out whether the type parameter is an Option or not. I have read some blogposts, i.e. this one, about type classes in scala recently, so I came up with this solution: case class OptionFinder[A](isOption: Boolean) implicit def notOption[A]: OptionFinder[A] = OptionFinder(false) implicit def hitOption[A]: OptionFinder[Option[A]] = OptionFinder(true) def myFunction[A](value: A)(implicit optionFinder: OptionFinder[A]): String = { if