How to define “type disjunction” (union types)?

前端 未结 15 2236
温柔的废话
温柔的废话 2020-11-22 05:52

One way that has been suggested to deal with double definitions of overloaded methods is to replace overloading with pattern matching:

object Bar {
   def fo         


        
15条回答
  •  鱼传尺愫
    2020-11-22 06:28

    There is another way which is slightly easier to understand if you do not grok Curry-Howard:

    type v[A,B] = Either[Option[A], Option[B]]
    
    private def L[A,B](a: A): v[A,B] = Left(Some(a))
    private def R[A,B](b: B): v[A,B] = Right(Some(b))  
    // TODO: for more use scala macro to generate this for up to 22 types?
    implicit def a2[A,B](a: A): v[A,B] = L(a)
    implicit def b2[A,B](b: B): v[A,B] = R(b)
    implicit def a3[A,B,C](a: A): v[v[A,B],C] = L(a2(a))
    implicit def b3[A,B,C](b: B): v[v[A,B],C] = L(b2(b))
    implicit def a4[A,B,C,D](a: A): v[v[v[A,B],C],D] = L(a3(a))
    implicit def b4[A,B,C,D](b: B): v[v[v[A,B],C],D] = L(b3(b))    
    implicit def a5[A,B,C,D,E](a: A): v[v[v[v[A,B],C],D],E] = L(a4(a))
    implicit def b5[A,B,C,D,E](b: B): v[v[v[v[A,B],C],D],E] = L(b4(b))
    
    type JsonPrimtives = (String v Int v Double)
    type ValidJsonPrimitive[A] = A => JsonPrimtives
    
    def test[A : ValidJsonPrimitive](x: A): A = x 
    
    test("hi")
    test(9)
    // test(true)   // does not compile
    

    I use similar technique in dijon

提交回复
热议问题