Emulating! = In scala using <>

I'm trying to imitate! = With <> in Scala.

implicit def conditional[A](left : A) = new {
 |   def<>[A](right : A) = (left != right)
 | }

In which case this emulation will not work

+3
source share
2 answers

This should always work, but maybe not the way you imagine. Do you expect the two types to be the same? If so, it should be

class Conditionalize[A](left: A) { def <>(right: A) = left != right }
implicit def conditional[A](left: A) = new Conditionalize(left)

If not, then a clearer use of a separate type parameter:

implicit def notequality[A](a: A) = new { def <>[B](b: B) = a != b }

The first will only work if the LHS does not have to be implicitly converted to be of the same type as the RHS. Provided a conditional, but not definable value:

implicit def int_to_string(i: Int) = i.toString
scala> "5" <> 5
res0: Boolean = false

scala> 5 <> "5"
<console>:9: error: type mismatch;
 found   : java.lang.String("5")
 required: Int
       5 <> "5"

because you cannot cling to implications.

, ! =. (: null, - null.)

, . Scala Java, 0.0 == -0.0, (new java.lang.Double(0.0)).equals(new java.lang.Double(-0.0)) , .

, @specialized, .

+7

, <> , ==, . :

a <> b >> 2  // (a <> b) >> 2
a != b >> 2  // a != (b >> 2)
+4

Source: https://habr.com/ru/post/1770608/


All Articles