forall in Scala

后端 未结 5 1887
别那么骄傲
别那么骄傲 2020-12-02 05:57

As shown below, in Haskell, it\'s possible to store in a list values with heterogeneous types with certain context bounds on them:

data ShowBox = forall s. S         


        
5条回答
  •  醉酒成梦
    2020-12-02 06:55

    Why not:

    trait ShowBox {
        def show: String
    }
    
    object ShowBox {
        def apply[s](x: s)(implicit i: Show[s]): ShowBox = new ShowBox {
            override def show: String = i.show(x)
        }
    }
    

    As the authorities' answers suggested, I'm often surprised that Scala can translate "Haskell type monsters" into very simple one.

提交回复
热议问题