How to make method return the same generic as the input?

前端 未结 3 1113
面向向阳花
面向向阳花 2021-02-07 10:20

I want to split a string delimited by commas and use the result as either a Seq or a Set:

def splitByComma(commaDelimited: String): Arr         


        
3条回答
  •  隐瞒了意图╮
    2021-02-07 10:42

    There's a simple workaround for this. Not exactly the requested syntax but just as concise and it should be 2.13 compatible.

    def simpleSplitByComma(coll :Iterable[String]) =
      coll.flatMap(_.trim.split(","))
    
    simpleSplitByComma(Set("hello,world"))          //res0: Set(hello, world)
    simpleSplitByComma(Seq("bellow,world"))         //res1: List(bellow, world)
    simpleSplitByComma(Array("fellow,old"))         //res2: ArrayBuffer(fellow, old)
    simpleSplitByComma(Stream("end,of,the,world"))  //res3: Stream(end, ?)
    

提交回复
热议问题