How is scalaz able to do "A \/ B", and how can I do my own "B.??" or "A <??> B"

69 Views Asked by At

I am relatively new to scala implicits, but if I wanted to be able to do 5.??, I would do:

class MyInt(int: Int) {
  // Useless, just to make a point
  def ?? : Int = int * 100
}

implicit def toMyInt(int: Int): MyInt =
  MyInt(int)

My question is: How is scalaz able to do A \/ B, and how could I do the same thing as above, adding my own operators (but to work with types)?
A little bit of context about what Im trying to play around and do:

abstract class Err(val message: String) { /* ... */ }

sealed trait MaybeErr[+E <: Err, +T] { /* ... */ }

The closest I have been able to get:

type ??[+T] = MaybeErr[Err, T]

type <??>[+E <: Err, +T] = MaybeErr[E, T]

def div1(t: Int, b: Int): ??[Int] = /* ... */

def div2(t: Int, b: Int): <??>[OtherErr, Int] = /* ... */

What I want to be able to do:

// T.?? = MaybeErr[Err, T]

// E <??> T = MaybeErr[E, T]

def div1(t: Int, b: Int): Int.?? = /* ... */

def div2(t: Int, b: Int): OtherErr <??> Int = /* ... */
1

There are 1 best solutions below

0
Jörg W Mittag On

How is scalaz able to do A \/ B, and how could I do the same thing as above

Any binary type constructor C[A, B] can alternatively be written in infix form A C B (or C:[A, B] as B C: A for type constructors ending in a : colon), see section 3.10 Infix Types of the Scala 2.13 Language Specification.

You can see the definition of \/ in core/src/main/scala/scalaz/Either.scala:

sealed abstract class \/[A, B] extends Product with Serializable