Scala:“ var”和“ def”之间的区别是什么?

时间:2019-06-04 15:46:36

标签: scala list function for-loop

我能够创建一个打印浮点数的函数

val printList: ((Double, Double, Double) => Unit) = (f1: Double, f2: Double, f3: Double ) => {
    println(f1)
    println(f2)
    println(f3)
}
printList (0.01, 0.2, 1)

我想知道如何使用float列表(大小可以更改)来做到这一点。 我找到了使用“ def”功能的解决方案

def printList2(list: List[Double]): Unit = {
    for (f <- list) println(f)
}
printList2 (List(0.01, 0.2, 1))

这是我的问题:

- what is the difference between val and def?
- Can we use List with val? 

我尝试将列表与var结合使用,但出现一些错误

第一次尝试:

val printList3(list: List[Double]): Unit = {
    for (f <- list) println(f)
}
// <console>:23: error: not found: value printList3

第二次尝试:

var printList4((List[Double]) => Unit) = (list: List[Double]) =>{
    for (f <- list) println(f)
}
// <console>:24: error: not found: value list

0 个答案:

没有答案
相关问题