Questions tagged [unapply]

37 questions
43
votes
3 answers

What is the difference between unapply and unapplySeq?

Why does Scala have both unapply and unapplySeq? What is the difference between the two? When should I prefer one over the other?
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
21
votes
2 answers

Scala: Case class unapply vs a manual implementation and type erasure

I'm trying to understand what Scala does with Case Classes that makes them somehow immune to type erasure warnings. Let's say we have the following, simple class structure. It's basically an Either: abstract class BlackOrWhite[A, B] case class…
Nycto
  • 1,670
  • 2
  • 14
  • 18
21
votes
3 answers

How to pattern match a class with multiple argument lists?

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, _, _) =>…
soc
  • 27,983
  • 20
  • 111
  • 215
19
votes
2 answers

What are the limitations on inference of higher-kinded types in Scala?

In the following simplified sample code: case class One[A](a: A) // An identity functor case class Twice[F[_], A](a: F[A], b: F[A]) // A functor transformer type Twice1[F[_]] = ({type L[α] = Twice[F, α]}) // We'll use Twice1[F]#L when we'd like to…
17
votes
5 answers

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

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…
Dan Shryock
  • 345
  • 2
  • 9
15
votes
1 answer

Can a Scala "extractor" use generics on unapply?

Can't I use a generic on the unapply method of an extractor along with an implicit "converter" to support a pattern match specific to the parameterised type? I'd like to do this (Note the use of [T] on the unapply line), trait StringDecoder[A] { …
Toby
  • 9,523
  • 8
  • 36
  • 59
13
votes
1 answer

Scala - can unapply return varargs?

Object L1 below works. I can "create" an L1 by passing in varargs, which is nice, but I would like to be able to assign to an L1 using the same syntax. Unfortunately, the way I've done it here requires the uglier syntax of nesting an Array inside…
dhg
  • 52,383
  • 8
  • 123
  • 144
9
votes
1 answer

Scala - implicit conversion with unapply

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…
dhg
  • 52,383
  • 8
  • 123
  • 144
8
votes
1 answer

Scala - add unapply to Int

I want to be able to do this: scala> val Int(i) = "1" i: Int = 1 But Int doesn't have an unapply method. I found this answer which gives instructions on how to implicitly add a method to an existing object, so I tried it out. The solution they…
dhg
  • 52,383
  • 8
  • 123
  • 144
5
votes
4 answers

Does Scala allow for this kind of extractor?

Let's say I have this collection: val a = Array(Array(1,2,3,4,5),Array(4,5),Array(5),Array(1,2,6,7,8)) Is there a way to define an extractor which would work in the following way: a.foreach(e => { e match { case Array( ending with 5 ) => …
Senthess
  • 17,020
  • 5
  • 23
  • 28
5
votes
1 answer

How to use extractor in polymorphic unapply?

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…
Marvin.Hansen
  • 1,436
  • 1
  • 15
  • 29
4
votes
1 answer

understanding unapply without case class

I am trying out below example to understand unapply, class Emp(name: String, age: Int) object Emp { def apply(name: String, age: Int): Emp = new Emp(name, age) def unapply(emp: Emp): Option[(String, Int)] = Some(emp.name,…
mukesh210
  • 2,792
  • 2
  • 19
  • 41
3
votes
1 answer

Scala unapply method

I am trying to understand the scala unapply method. Below is my understanding. Say if I have a Person object: class Person(val fname: String, val lname: String) object Person{ def unapply(x: Person) : Option[(String, String)] = …
ftw
  • 365
  • 2
  • 6
  • 16
3
votes
1 answer

What is the time and space complexity of a Scala's head/tail extractor?

What is the time and space complexity for this: def isPalindrome[A](x: Seq[A]): Boolean = x match { case h +: middle :+ t => h == t && isPalindrome(middle) case _ => true } Does it depend on the implementation of Seq? Since IndexedSeq should…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
3
votes
1 answer

What are the correct apply and unapply methods to avoid this java.lang.ClassCastException error in a Scala Play app?

I'm building a Scala Play app where events and data are persisted in Json format, and I'm trying to model users and the roles they're assigned. My thinking has been to model Roles as case objects, as each standard role only needs defining once for…
1
2 3