extractor

Scala, partial functions

拈花ヽ惹草 提交于 2019-12-10 03:24:15
问题 Is there any way to create a PartialFunction except through the case statement? I'm curious, because I'd like to express the following (scala pseudo ahead!)... val bi = BigInt(_) if (bi.isValidInt) bi.intValue ... as a partial function, and doing val toInt : PartialFunction[String, Int] = { case s if BigInt(s).isValidInt => BigInt(s).intValue } seems redundant since I create a BigInt twice. 回答1: Not sure I understand the question. But here's my attempt: Why not create an extractor? object

How to use extractor in polymorphic unapply?

旧巷老猫 提交于 2019-12-09 18:31:51
问题 I don't really get this little thingy. I have an abstract class Box with several sub-classes for different types. For example abstract class Box class StringBox(val sValue : String) extends Box The apply method in the companion object for Box is simple: object Box{ def apply(s: String) = new StringBox(s) def apply(b: Boolean) = new BooleanBox(b) def apply(d: Double) = new DoubleBox(d) } so I can write val sb = Box("StringBox) Okay, writing unapply makes some trouble. My first idea was to use

Implicit parameters won't work on unapply. How to hide ubiquitous parameters from extractors?

此生再无相见时 提交于 2019-12-05 16:15:28
Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting parameter a, and a disturbingly ubiquitous parameter b that would be nice to hide away, when extracting c. [ EDIT ]: It appears something was broken in my intellij/scala-plugin installation that caused this. I cannot explain. I was having numerous strange problems with my intellij lately. After reinstalling, I can no longer reprodce my problem. Confirmed that unapply/unapplySeq do allow for implicit parameters! Thanks for your help. This does not work (**EDIT :yes, it does):**

Scala, partial functions

自古美人都是妖i 提交于 2019-12-05 03:32:00
Is there any way to create a PartialFunction except through the case statement? I'm curious, because I'd like to express the following (scala pseudo ahead!)... val bi = BigInt(_) if (bi.isValidInt) bi.intValue ... as a partial function, and doing val toInt : PartialFunction[String, Int] = { case s if BigInt(s).isValidInt => BigInt(s).intValue } seems redundant since I create a BigInt twice. Not sure I understand the question. But here's my attempt: Why not create an extractor? object ValidBigInt { def unapply(s: String): Option[Int] = { val bi = BigInt(s) if (bi.isValidInt) Some(bi.intValue)

Difference between home made extractor and case class extractor

懵懂的女人 提交于 2019-12-05 00:43:32
问题 According to the scala specification, the extractor built by case classes is the following (scala specification §5.3.2): def unapply[tps](x: c[tps]) = if (x eq null) scala.None else scala.Some(x.xs11, ..., x.xs1k) For implementation reasons, I want to be able to mimic the behavior of this extractor on a non-case class. However, my implementation fails to reproduce the same behavior. Here is an example of the difference i have: trait A sealed trait B[X <: A]{ val x: X } case class C[X <: A](x:

How to use extractor in polymorphic unapply?

十年热恋 提交于 2019-12-04 07:16:55
I don't really get this little thingy. I have an abstract class Box with several sub-classes for different types. For example abstract class Box class StringBox(val sValue : String) extends Box The apply method in the companion object for Box is simple: object Box{ def apply(s: String) = new StringBox(s) def apply(b: Boolean) = new BooleanBox(b) def apply(d: Double) = new DoubleBox(d) } so I can write val sb = Box("StringBox) Okay, writing unapply makes some trouble. My first idea was to use pattern matching on the type, like this this: def unapply(b: Box) = b match { case sb: StringBox =>

Scala: Pattern matching when one of two items meets some condition

浪子不回头ぞ 提交于 2019-12-04 02:04:10
I'm often writing code that compares two objects and produces a value based on whether they are the same, or different, based on how they are different. So I might write: val result = (v1,v2) match { case (Some(value1), Some(value2)) => "a" case (Some(value), None)) => "b" case (None, Some(value)) => "b" case _ = > "c" } Those 2nd and 3rd cases are the same really, so I tried writing: val result = (v1,v2) match { case (Some(value1), Some(value2)) => "a" case (Some(value), None)) || (None, Some(value)) => "b" case _ = > "c" } But no luck. I encounter this problem in a few places, and this is

Is it possible to use implicit conversions for parameters to extractors (unapply) in Scala?

大城市里の小女人 提交于 2019-12-01 12:35:05
I have created a class called CaseInsensitive which wraps a string (see Implementing a string class that does case insensitive comparisions in Scala ). I've created a case class which has a member variable of type CaseInsensitive, so it gets a default unapply method, which extracts a variable of type CaseInsensitive, but I was hoping to use it like this: case class PropertyKey( val name : CaseInsensitive ) val foo = new PropertyKey("foo") val result = foo match { case PropertyKey("foo") => true case _ => false } This code fails to compile: (on the extractor line, not the constructor line) type

unapply method of a case class is not used by the Scala compiler to do pattern matching, why is that?

坚强是说给别人听的谎言 提交于 2019-12-01 06:42:00
问题 abstract class Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal Say I have defined Cat and Dog, two case classes. Then I use them like this: val animal = createAnimal animal match { case Dog(anyName) => "this is a dog" case Cat("kitty") => "this is a cat named kitty" case _ => "other animal" } If I decompile the bytecode to Java, I get something like this: Animal animal = createAnimal(); String result = "other animal"; if (animal instanceof Dog) {

Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala

≡放荡痞女 提交于 2019-11-30 18:57:25
I have a simple class hierarchy that represents a graph-like structure with several distinct types of vertexes implemented using case classes: sealed trait Node sealed abstract case class Vertex extends Node case class Arc extends Node case class VertexType1 (val a:Int) extends Vertex case class VertexType2 (val b:Int) extends Vertex This allows me to write match blocks like this: def test (x: Node) = x match { case _ : Arc => "got arc" case _ : Vertex => "got vertex" } or like this: def test (x: Node) = x match { case _ : Arc => "got arc" case c : Vertex => c match { case _ : VertexType1(a) =