This question was previously asked in the Scala-user mailing list without any reported answer.
Scala> Val T = New pair (1, 2) {Override DRF equivalent (OBG: A) = obj.isInstanceOf [(Int, Int)] & amp; Amp; Obj.asInstanceOf [(Int, Int)] ._ 1 == this._1}} T: (int, int) = (1,2) scale & gt; T match {case (1, 1) = & gt; Println ("match") case _ = & gt; Println ("did not match")} did not match skeleton & gt; (1, 1) match {case T = & gt; Println ("match") case _ = & gt; Println ("did not match")} did not match skeleton & gt; T == (1, 1) res15: boolean = true
I thought that a continuous (matching) pattern matching depends on the value of "par", but the results show. If this is not a case, then what is the benchmark?
Someone suggested that matter (1, 1) =>
is a extractor pattern and it uses Tuple2.unapply instead
so I tried to:
scala> Pair. Nupali (T) Race 1: Option [(Inte, Init)] = Some ((1,2)) Scale & gt; Please explain to anyone why ==
may be correct but I can not.
The problem with your example is that you only equal code> anonymous class By using the method you define your specific tubal. Take a closer look at what you are doing when you run the code provided. val p = new pair (1, 2) {override def equal (obj: Any) = {Obj.isInstanceOf [(Int, Int)] & amp; Amp; Obj.asInstanceOf [(Int, Int)] ._ 1 == this._1}}
What Scala does here makes it a new anonymous square which increases the pair Is
and overrides its equivalent. So this is equivalent to running the following code:
increases the class FU pair (1,2) {override def equal to (obj: Any) = {obj.isInstanceOf [(Int, Int) ] & Amp; & Amp; Obj.asInstanceOf [(Int, Int)] ._ 1 == this._1}} val p = new Foo
And this is where you really are where the problem lies! The definition of equal
is not symmetrical p == (1,1)
to true
and (1,1) == p < / Code> evaluates false
! The reason for this is that the former p.equals ((1,1)) is equivalent to
, while the latter (1,1). Equivalent (P)
is equivalent to. In the example given by you, this does not work because in this case the object is compared to the object, and not the other way. Therefore, as you have indicated, Pair.unapply (p) .get == (1, 1) evaluates
to true
, even then (1,1) = for false
, and it seems that the latter is used to match.
However, in any case not to be emulated which is not symmetrical is actually a very bad idea because the code execution depends on the order in which you compare the object We do. Apart from this, there is another problem in your defined equilibrium - it fails p
from any pair
to not type (int , Int)
error while trying to compare. The reason for this is that, after bleaching type (which is applied by JVM J. Generics), a pair
is no longer parametridge by the types of its components. Therefore, the (int, int)
is exactly the same type as (string, string)
and thus, the following code will fail by error:
Scala as a (string, string)
as p == ("foo", "bar")
> Will try to enter (Int, Int)
.
If you want to implement this functionality, the easiest thing you can do is use the broker my library pattern, pair
. However, you should not call your method equal
to call it something else, such as ~ =
. I have to go now, but when I come back, I can give you a code for this. It is very easy for you to see the implementation of equal
in a pair and should take part that compares another argument :)
Comments
Post a Comment