Minimal framework in Scala for collections with inheriting return type

后端 未结 3 2301
旧时难觅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:07

    Edit: I just realized that Rex had a concrete class Novel in his example, not a trait as I've used below. The trait implementation is a bit too simple to be a solution to Rex's question, therefore. It can be done as well using a concrete class (see below), but the only way I could make that work is by some casting, which makes this not really 'compile time type-safe'. This So this does not qualify as a solution.

    Perhaps not the prettiest, but a simple example using abstract member types could be implemented as follows:

    
    trait Novel[A] { 
       type T <: Novel[A] 
       def reverse : T 
       def revrev : T#T = reverse.reverse 
    }
    
    class ShortStory[A](var story: String) extends Novel[A] {
     type T = ShortStory[A]
     def reverse : T = new ShortStory[A](story reverse)
     def myMethod: Unit = println("a short story method")
    }
    
    scala> val ss1 = new ShortStory[String]("the story so far")
    ss1: ShortStory[String] = ShortStory@5debf305
    
    scala> val ssRev = ss1 reverse 
    ssRev: ss1.T = ShortStory@5ae9581b
    
    scala> ssRev story
    res0: String = raf os yrots eht
    
    scala> val ssRevRev = ss1 revrev
    ssRevRev: ss1.T#T = ShortStory@2429de03
    
    scala> ssRevRev story
    res1: String = the story so far
    
    scala> ssRevRev myMethod
    a short story method
    

    It's certainly minimal, but I doubt whether this would enough to be used as a kind of framework. And of course the types returned not anywhere near as clear as in the Scala collections framework, so perhaps this might be a bit too simple. For the given case, it seems to do the job, however. As remarked above, this does not do the job for the given case, so some other solution is required here.

    Yet Another Edit: Something similar can be done using a concrete class as well, though that also not suffices to be type safe:

    
    class Novel[A](var story: String) {
      type T <: Novel[A] 
      def reverse: T = new Novel[A](story reverse).asInstanceOf[T]  
      def revrev : T#T = reverse.reverse
    }
    class ShortStory[A](var s: String) extends Novel[A](s) {
     type T = ShortStory[A]
     override def reverse : T = new ShortStory(story reverse)
     def myMethod: Unit = println("a short story method")
    }
    

    And the code will work as in the trait example. But it suffers from the same problem as Rex mentioned in his edit as well. The override on ShortStory is not necessary to make this compile. However, it will fail at runtime if you don't do this and call the reverse method on a ShortStory instance.

提交回复
热议问题