unapply

Scala shapeless Generic.Aux implicit parameter not found in unapply

旧城冷巷雨未停 提交于 2020-01-05 06:26:46
问题 I encountered the following problem with implicits in Scala, using Shapeless's Generic.Aux : case class Complex(re: Double, im: Double) object Prod2 { def unapply[C, A, B](c: C)(implicit C: Generic.Aux[C, A :: B :: HNil]) = Some((C.to(c).head, C.to(c).tail.head)) } val c = Complex(1.0, 2.0) val Prod2(re, im) = c The code above does not compile. It reports Error:(22, 7) could not find implicit value for parameter C: shapeless.Generic.Aux[nexus.ops.Test.Complex,A :: B :: shapeless.HNil] val

Can extractors be customized with parameters in the body of a case statement (or anywhere else that an extractor would be used)?

隐身守侯 提交于 2019-12-28 06:29:09
问题 Basically, I would like to be able to build a custom extractor without having to store it in a variable prior to using it. This isn't a real example of how I would use it, it would more likely be used in the case of a regular expression or some other string pattern like construct, but hopefully it explains what I'm looking for: def someExtractorBuilder(arg:Boolean) = new { def unapply(s:String):Option[String] = if(arg) Some(s) else None } //I would like to be able to use something like this

What is the difference between unapply and unapplySeq?

牧云@^-^@ 提交于 2019-12-20 08:56:25
问题 Why does Scala have both unapply and unapplySeq ? What is the difference between the two? When should I prefer one over the other? 回答1: Without going into details and simplifying a bit: For regular parameters apply constructs and unapply de-structures: object S { def apply(a: A):S = ... // makes a S from an A def unapply(s: S): Option[A] = ... // retrieve the A from the S } val s = S(a) s match { case S(a) => a } For repeated parameters, apply constructs and unapplySeq de-structures: object M

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

≯℡__Kan透↙ 提交于 2019-12-19 10:51:01
问题 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") =>

Scala - implicit conversion with unapply

时光怂恿深爱的人放手 提交于 2019-12-10 02:58:59
问题 I'd like an extractor to implicitly convert its parameters, but it doesn't seem to work. Consider this very simple case: case class MyString(s: String) {} implicit def string2mystring(x: String): MyString = new MyString(x) implicit def mystring2string(x: MyString) = x.s object Apply { def unapply(s: MyString): Option[String] = Some(s) } But I'm not able to use it as I would expect: val Apply(z) = "a" // error: scrutinee is incompatible with pattern type Can anyone explain why it fails to

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

How to pattern match a class with multiple argument lists?

北战南征 提交于 2019-12-08 14:37:59
问题 Consider this class: class DateTime(year: Int, month: Int, day: Int)(hour: Int, minute: Int, second: Int) how would the unapply method look like, if I would like to match against something like: dt match { case DateTime(2012, 12, 12)(12, _, _) => // December 12th 2012, 12 o'clock /* ... */ } I tried this: def unapply(dt: DateTime) = Some((dt.year, dt.month, dt.day),(dt.hour, dt.minute, dt.second)) But that didn't really work. 回答1: Case classes match (and do their other nifty things) only on

Scala - implicit conversion with unapply

眉间皱痕 提交于 2019-12-05 04:13:28
I'd like an extractor to implicitly convert its parameters, but it doesn't seem to work. Consider this very simple case: case class MyString(s: String) {} implicit def string2mystring(x: String): MyString = new MyString(x) implicit def mystring2string(x: MyString) = x.s object Apply { def unapply(s: MyString): Option[String] = Some(s) } But I'm not able to use it as I would expect: val Apply(z) = "a" // error: scrutinee is incompatible with pattern type Can anyone explain why it fails to convert the parameter from String to MyString ? I would expect it to call string2mystring("a") on the fly.

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 =>

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