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 = /* ... */
Any binary type constructor
C[A, B]can alternatively be written in infix formA C B(orC:[A, B]asB C: Afor 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
\/incore/src/main/scala/scalaz/Either.scala: