implicit

scala playframework json implicit case class conversion

蹲街弑〆低调 提交于 2020-01-05 12:09:06
问题 I am developing my first Play 2.1 application in Scala. The task I am trying to accomplish is to parse json into 3 different case classes. The problem is - I do not know where to declare all case classes. Each class in it's own file or all in one file. Here is what I've done (it doesn't work, case values are not visible in controller object): File LoginBase.scala package models abstract class LoginBase case class Login(email: String, password: String) extends LoginBase case class RestoreLogin

NPE in spray-json because of recursive implicits (context bound issue?)

末鹿安然 提交于 2020-01-04 05:22:09
问题 Perhaps I discovered a bug in spray-json. I get Null Pointer Exception when I'm trying to get json of an object that has field of type of itself. Example is: case class TestItem(subitems: Option[List[TestItem]]) object MyJsonProtocol extends DefaultJsonProtocol { implicit val testItemFormat: RootJsonFormat[TestItem] = jsonFormat(TestItem, "subitems") } import MyJsonProtocol._ object TestNPE { def main(args: Array[String]) { val subitems = List(TestItem(None)) val item: TestItem = TestItem

Can implicits be used to disambiguate overloaded definition?

。_饼干妹妹 提交于 2020-01-04 04:53:17
问题 Consider the following overloaded definition of method mean : def mean[T](data: Iterable[T])(implicit number: Fractional[T]): T = { import number._ val sum = data.foldLeft(zero)(plus) div(sum, fromInt(data.size)) } def mean[T](data: Iterable[T])(implicit number: Integral[T]): Double = { import number._ val sum = data.foldLeft(zero)(plus) sum.toDouble / data.size } I would like second definition which returns Double only to be used in the case of Integral types, however mean(List(1,2,3,4))

Why does Int not inherit/extend from Ordered[Int]

孤者浪人 提交于 2020-01-03 10:44:53
问题 I have a question on type design. Why does Int not extend the Ordered trait. Isn't Int ordered by nature? Instead, the scala library provides implicit 'orderer' methods which convert Int to Ordered[Int]. What are the design choices being made here? Example taken from the book Programming in Scala def maxListImpParm[T <% Ordered[T]](elements:List[T]):T= ... maxListImpParm(List(1,5,10,3)) // works because of implicit methods 回答1: Because Int (and some other classes inherited from AnyVal) is

solution of an implicit equation with fzero on MATLAB

穿精又带淫゛_ 提交于 2020-01-03 03:10:50
问题 I've been trying to solve this implicit equation by using fzero in MATLAB. File that holds the code is named as "colebrook" and I've typed so far is as below. D = input('Please enter the pipe diameter in meters: '); V = input('Please enter the fluid velocity in m/s: '); rho = input('Please enter fluid density in kg/m^3: '); mew = input('Please enter fluid viscosity in kg/m*s: '); Re = D*V*rho/mew; eps = input('Enter absolute roughness in milimeters: '); eD = eps/(D*1000); a = fzero

implicit parameter VS default parameter value

China☆狼群 提交于 2020-01-02 05:12:06
问题 There are, at least, two techniques in Scala to pass default value to a method 1) default parameter value scala> def f(i: Int = 0) = i f: (i: Int)Int scala> f() res0: Int = 0 scala> f(1) res1: Int = 1 2) implicit parameter scala> def g(implicit i: Int) = i g: (implicit i: Int)Int scala> implicit val default = 0 default: Int = 0 scala> g(1) res5: Int = 1 scala> g res7: Int = 0 In which case do you choose one or another ? With the power of implicit, default values are they a usefull feature ?

Delphi (-XE) : casting to a record type with implicit conversion

倖福魔咒の 提交于 2020-01-02 04:40:10
问题 I have a record type with methods, representing an specific hardware measurement type, read from the instrument as a string. The record contains implicit coversion to (and from) a string. If I cast a string as a record type, it seems to work, but is this safe? That is, does casting a string to a record with implicit string conversion call the implicit conversion as per assigning a temporary value? var a: MeasurementRecord; // record type with implicit string conversion & decode methods b:

Scala - Can implicitNotFound annotation be applied at the method level?

試著忘記壹切 提交于 2020-01-02 03:54:07
问题 I have a method that takes type parameters with an implicit view bounds on them. Can I use the @implicitNotFound annotation to give nicer compiler errors when the method is called with invalid data types? The documentation for the method is useless and even the source code doesn't help, and all the examples of use online are at the trait or class level. 回答1: No, you cannot directly do that. As you’ve noticed, @implicitNotFound annotates traits or classes. You could, however, make a special

Scala implicit conversion scope issues

倖福魔咒の 提交于 2020-01-01 05:24:11
问题 Take this code: class Register(var value:Int = 0) { def getZeroFlag() : Boolean = (value & 0x80) != 0 } object Register { implicit def reg2int(r:Register):Int = r.value implicit def bool2int(b:Boolean):Int = if (b) 1 else 0 } I want to use it like so: val x = register.getZeroFlag + 10 but I am greeted with: type mismatch; found : Boolean required: Int What goes? Do I need to define a implicit taking a function that returns a bool? 回答1: Here's an example demonstrating how to use your implicits

How to override an implicit value?

。_饼干妹妹 提交于 2020-01-01 05:15:06
问题 Suppose I have the code: class A(implicit s:String = "foo"){println(s)} object X { implicit val s1 = "hello" } object Y { import X._ // do something with X implicit val s2 = "hi" val a = new A } I get the error: <console>:14: error: ambiguous implicit values: both value s2 in object Y of type => String and value s1 in object X of type => String match expected type String val a = new A Is there any way I can tell Scala to use the value s2 in Y ? (if I rename s2 to s1 , it works as expected but