【发布时间】:2023-03-29 04:57:01
【问题描述】:
我是一个学习scala的新手。
请问如何查看函数返回值类型?
例如:
def decode(list :List[(Int, String)]):List[String] = {
//val result = List[String]()
//list.map(l => outputCharWithTime(l._1,l._2,Nil))
//result
excuteDecode(list,List[String]())
def excuteDecode(list:List[(Int,String)],result:List[String]):List[String] = list match {
case Nil => Nil
case x::Nil=>outputCharWithTime(x._1,x._2,result)
case x::y =>excuteDecode(y,outputCharWithTime(x._1,x._2,result))
}
def outputCharWithTime(times:Int,str:String , result :List[String]):List[String]={
times match{
case 0 => result
case x => outputCharWithTime(times-1,str,str::result)
}
}
}
在这段代码中,所有的函数返回类型都设置为List[String],还为excuteDecode()函数创建了一个空的List[String]参数。
但是我得到一个编译错误:
错误:(128, 5) 类型不匹配; 发现:单位 必需:列表[字符串] }
谁能告诉我为什么存在问题以及如何自己检查实际的返回类型?
【问题讨论】:
标签: scala user-defined-functions scala-collections