Multiple lower type bounds in Scala

后端 未结 2 1884
你的背包
你的背包 2021-02-13 14:35

I noticed that tuple.productIterator always returns an Iterator[Any] an wondered if it\'s not possible to set multiple lower bounds (so it could be an

2条回答
  •  既然无缘
    2021-02-13 15:21

    It sounds like what you need is an HList: http://apocalisp.wordpress.com/2010/07/06/type-level-programming-in-scala-part-6a-heterogeneous-list%C2%A0basics/

    To answer the specific question:

    scala> def f[T, A <: T,B <: T](a:A, b:B) = List[T](a,b)
    f: [T, A <: T, B <: T](a: A, b: B)List[T]
    
    scala> f(1, true)
    res0: List[AnyVal] = List(1, true)
    
    scala> f("x", "y")
    res1: List[java.lang.String] = List(x, y)
    

提交回复
热议问题