Questions tagged [unapply]

37 questions
3
votes
1 answer

Scala custom unapply with generics

I would like to condense my evaluator thanks to a custom generic unapply function, which evaluates the argument and returns the value if it succeeds. But this fails with the error error: not found: type Eval Any way of achieving this? I have looked…
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
3
votes
2 answers

scala unapply that returns boolean

Reading this, I still have questions about unapply() that returns Boolean. If take a look at Scala Programming Book (2nd edition), page 602. There is an example: case Email(Twice(x @ UpperCase()), domain) => ... Where UpperCase defined as an object…
ses
  • 13,174
  • 31
  • 123
  • 226
2
votes
1 answer

Why is there a `null` check in the implementation of the `unapply` method for case classes?

I'm working to replace the unapply method on a case class's companion object with my own implementation. And after investigating lots of different tangents related to implementing unapply, it appears there is a null guard in most of them, both in…
2
votes
1 answer

why do I have to use the unapply operator in maple, when defining a function by diff?

I am an absolute beginner in maple and have problems to understand the following: The following does not work: f:=(x)->x^2; df_wrong:=(x)->diff(f(x),x); Since df_wrong(1); always yields the following "Error, (in df_wrong) invalid input: diff…
DonkeyKong
  • 465
  • 1
  • 5
  • 16
2
votes
1 answer

scala Nil type matchs against arraybuffer

I found a strange working construct in scala: (ArrayBuffer[Int]():Seq[Int]) match { case Nil => "whoo" case _ => "nayyy" } which returns "whoo" Apparently this already works partially for Vectors, but not pattern matching. can someone…
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
2
votes
2 answers

Casting String to Int using scala extractors

I am trying to cast a String to Int using extractors. My code looks as follows. object Apply { def unapply(s: String): Option[Int] = try { Some(s.toInt) } catch { case _: java.lang.Exception => None } } object App { def toT[T](s:…
vikraman
  • 358
  • 7
  • 14
2
votes
2 answers

Scala match error with unapply

I'm trying out the code at http://www.scala-lang.org/node/112 and I'm getting a match error for something that doesn't look like it should throw one. This is the original code: object Twice { def apply(x: Int): Int =…
JasonMond
  • 1,440
  • 1
  • 16
  • 30
1
vote
2 answers

Scala shapeless Generic.Aux implicit parameter not found in unapply

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]) =…
Tongfei Chen
  • 613
  • 4
  • 14
1
vote
2 answers

Why can't I reuse "unapply" without repeating the method signature

The following Scala code compiles fine: val f = (input: String) => Some("result") object Extract { def unapply(input: String): Option[String] = f(input) } val Extract(result) = "a string" But if I replace the extractor by: object Extract { …
Victor Basso
  • 5,556
  • 5
  • 42
  • 60
1
vote
1 answer

Scala - not a case class nor does it have method .unapply

I am quite new to Scala and got a few unresolved problems with the following code: object exprs{ println("Welcome to the Scala worksheet") def show(e: Expr): String = e match { case Number(x) => x.toString case…
mj1261829
  • 1,200
  • 3
  • 26
  • 53
1
vote
2 answers

Scala implicit unapply

Am trying to implicitly deconstruct a class instance into a tuple to create a nicer DSL syntax. Here's a simplified example of what I'm trying to do: class Pair[A,B](a: A, b: B){ def left = a def right = b } val pair = new Pair(1,2) implicit def…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
1
vote
1 answer

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

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…
waterlooalex
  • 13,642
  • 16
  • 78
  • 99
1
vote
0 answers

Scala-Lift: ambiguous reference to overloaded definition - unapply method in child class

Compiler says error: ambiguous reference to overloaded definition, both method unapply in object UserDto of type (in: Any)Option[(String, String, String, String)] and method unapply in class AbstractJsonConversion of type (in:…
0
votes
1 answer

Why does this compile? (Scala Regex unapplySeq)

Why does this compile? val regex = raw"a*".r def matchRegex(str: String): Boolean = str match { case regex("abc") => true case _ => false } As you can see, I don't try to extract any values from the string but…
Devigny
  • 3
  • 2
0
votes
2 answers

Syntactic sugar explanation of Scala'a unapply method

I am getting an error in the extractor step (unapply method call). The error message is: Wrong number of arguments for the extractors. found 2; expected 0 Can someone please help what is causing the error (where my misunderstanding is). class…
user3103957
  • 636
  • 4
  • 16