case-class

Case class constructor argument type depending on the previous argument value

孤街浪徒 提交于 2019-11-30 01:16:26
问题 I am trying to do the following trait Stateful { type State } case class SystemState(system: Stateful, state: system.State) // does not compile That is, the type of state depends on (the value of) system . That, however, is not supported: illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one With function arguments, I could split the arguments into two argument lists, which is not possible with a case class constructor: def f

Scala case class private constructor but public apply method

流过昼夜 提交于 2019-11-29 22:55:43
If I have the following case class with a private constructor and I can not access the apply-method in the companion object. case class Meter private (m: Int) val m = Meter(10) // constructor Meter in class Meter cannot be accessed... Is there a way to use a case class with a private constructor but keep the generated apply-method in the companion public? I am aware that there is no difference (in my example) between the two options: val m1 = new Meter(10) val m2 = Meter(10) but I want to forbid the first option. -- edit -- Surprisingly the following works (but is not really what i want): val

How to use scala macros to create a function object (to create a Map[String, (T) => T])

。_饼干妹妹 提交于 2019-11-29 11:45:33
I am trying to use Scala macros to create a case class map of single-parameter copy methods, with each method accepting a Play Json JsValue and a case class instance, and returning an updated copy of the instance. However, I am running into problems with the macro syntax for returning a function object. Given a case class case class Clazz(id: Int, str: String, strOpt: Option[String]) the intention is to create a map of the class's copy methods implicit def jsonToInt(json: JsValue) = json.as[Int] implicit def jsonToStr(json: JsValue) = json.as[String] implicit def jsonToStrOpt(json: JsValue) =

How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?

♀尐吖头ヾ 提交于 2019-11-29 11:08:05
I'm making a Scala app that sets by reflection field values. This works OK. However, in order to set field values I need a created instance. If I have a class with an empty constructor, I can do this easily with classOf[Person].getConstructors.... However, when I try doing this with a Case class with a non empty constructor It doesn't work. I have all of the field names and its values, as well as the Object type I need to create. Can I instance the Case Class somehow with what I've got? The only thing I don't have is the parameter names from the Case Class constructor or a way to create this

Extract label values from a LabelledGeneric instance

∥☆過路亽.° 提交于 2019-11-29 03:11:45
Consider the following example: import shapeless._ case class Foo(bar: String, baz: Boolean) val labl = LabelledGeneric[Foo] Now, the type of labl is (prettified) LabelledGeneric[Foo] { type Repr = FieldType[Symbol @@ String("bar"), String] :: FieldType[Symbol @@ String("baz"), Boolean] :: HNil } which already conveys the information I need, i.e. the names of the case class fields. What I'm looking for is a way to go from labl to something along the lines of "bar" :: "baz" :: HNil i.e. materializing the information contained in the singleton types into a value. Is this possible? I could use a

Map flatten and flatmap not equivalent

若如初见. 提交于 2019-11-29 01:34:58
I thought that Scala construct map(f).flatten was equivalent to flatMap(f) . But with this example, it is not the case. I wonder what is the role of the case class in it. If I use integers, both are equivalent. But in my case, I cannot. case class CTest(v: Int) val s = Set(Map(CTest(0) -> List(0, 3), CTest(1) -> List(0, 2))) val possibilities = s flatMap { m => val mapping = m flatMap { case (label, destNodes) => destNodes map { case nodes => (label, nodes) } } mapping } possibilities Yields Set((CTest(0),3), (CTest(1), 2)) whereas case class CTest(v: Int) val s = Set(Map(CTest(0) -> List(0, 3

How do I “get” a Scala case object from Java?

▼魔方 西西 提交于 2019-11-28 22:32:30
I created a hierarchy of case objects in Scala that looks like the following: package my.awesome.package sealed abstract class PresetShapeType(val displayName: String) case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") case object DisplacementSensor extends PresetShapeType("Displacement Sensor") case object ForceSensor extends PresetShapeType("Force Sensor") case object PressureSensor extends PresetShapeType("Pressure Sensor") case object StrainSensor extends PresetShapeType("Strain Sensor") I also have a piece of Java code in which I'd like to access PressureSensor

rely on methods of case class in trait

爷,独闯天下 提交于 2019-11-28 21:22:39
Is there a way to rely on methods defined in case class in a trait? E.g., copy: the following doesn't work. I'm not sure why, though. trait K[T <: K[T]] { val x: String val y: String def m: T = copy(x = "hello") def copy(x: String = this.x, y: String = this.y): T } case class L(val x: String, val y: String) extends K[L] Gives: error: class L needs to be abstract, since method copy in trait K of type (x: String,y: String)L is not defined case class L(val x: String, val y: String) extends K[L] ^ I suppose that having method with name copy in trait instructs compiler to not generate method copy

Scala - how to print case classes like (pretty printed) tree

瘦欲@ 提交于 2019-11-28 17:45:37
I'm making a parser with Scala Combinators. It is awesome. What I end up with is a long list of entagled case classes, like: ClassDecl(Complex,List(VarDecl(Real,float), VarDecl(Imag,float))) , just 100x longer. I was wondering if there is a good way to print case classes like these in a tree-like fashion so that it's easier to read..? (or some other form of Pretty Print ) ClassDecl name = Complex fields = - VarDecl name = Real type = float - VarDecl name = Imag type = float ^ I want to end up with something like this edit: Bonus question Is there also a way to show the name of the parameter..?

Scala case class extending Product with Serializable

谁说我不能喝 提交于 2019-11-28 17:17:52
I am learning scala and tried following form Scala Cookbook: trait Animal trait FurryAnimal extends Animal case class Dog(name:String) extends Animal case class Cat(name:String) extends Animal Now when I did following as : val x = Array(Dog("Fido"),Cat("Felix")) it show result as : x:Array[Product with Serializable with Animal] = Array(Dog(Fido),Cat(Felix)) Although I know that a case class is mixed in with Product trait What I am not getting is : Product with Serializable with Animal As per my understanding Product has something to do with Pattern matching I did google it but didn't get