Minimal framework in Scala for collections with inheriting return type

后端 未结 3 2300
旧时难觅i
旧时难觅i 2021-02-19 04:48

Suppose one wants to build a novel generic class, Novel[A]. This class will contain lots of useful methods--perhaps it is a type of collection--and therefore you w

3条回答
  •  遇见更好的自我
    2021-02-19 05:06

    I haven't thought this through fully, but it type checks:

    object invariant {
      trait Novel[A] {
        type Repr[X] <: Novel[X]
    
        def reverse: Repr[A]
    
        def revrev: Repr[A]#Repr[A]
           = reverse.reverse
      }
      class ShortStory[A] extends Novel[A] {
        type Repr[X] = ShortStory[X]
    
        def reverse = this
      }
    
      val ss = new ShortStory[String]
      val ss2: ShortStory[String] = ss.revrev
    }
    
    object covariant {
      trait Novel[+A] {
        type Repr[X] <: Novel[_ <: X]
    
        def reverse: Repr[_ <: A]
    
        def revrev: Repr[_ <: A]#Repr[_ <: A] = reverse.reverse
      }
    
      class ShortStory[+A] extends Novel[A] {
        type Repr[X] = ShortStory[X]
    
        def reverse = this
      }
    
      val ss = new ShortStory[String]
      val ss2: ShortStory[String] = ss.revrev
    }
    

    EDIT

    The co-variant version can be much nicer:

    object covariant2 {
      trait Novel[+A] {
        type Repr[+X] <: Novel[X]
    
        def reverse: Repr[A]
    
        def revrev: Repr[A]#Repr[A] = reverse.reverse
      }
    
      class ShortStory[+A] extends Novel[A] {
        type Repr[+X] = ShortStory[X]
    
        def reverse = this
      }
    
      val ss = new ShortStory[String]
      val ss2: ShortStory[String] = ss.revrev
    }
    

提交回复
热议问题