3
나는 스칼라에 대한 튜토리얼을 시작하고 블록을 친다. 몇 가지 예를 병합하고 오류가 발생하지만 이유는 알 수 없습니다. (가) 아래의 유일한 차이는 날짜가 반환되는 반면, 일단 제의 frenchDate()
에 인쇄 및 인쇄된다는 것이다"오류 : 형식 불일치, 발견 : 유닛 필요 :() => 유닛"콜백에서
import java.text.DateFormat._
import java.util.{Date, Locale}
object FrenchDate {
def main(args: Array[String]) {
timer(frenchDate)
}
def frenchDate() {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
def timer(callback:() => Unit) {
while(true) {callback(); Thread sleep 1000}
}
}
작동하는 동안
import java.text.DateFormat._
import java.util.{Date, Locale}
object FrenchDate {
def main(args: Array[String]) {
timer(println(frenchDate))
}
def frenchDate():String = {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
df format now
}
def timer(callback:() => Unit) {
while(true) {callback(); Thread sleep 1000}
}
}
오류를
error: type mismatch;
found : Unit
required:() => Unit
println(frenchDate)
가져온다 첫 번째 콜백에서.
timer(println(frenchDate))
하는 println(frenchDate)
를 호출 timer
전달할으로 콜백 (Unit
있는) 반환 값을 사용하려고 :
(I 승 좋겠하지만 'timer {() => println (frenchDate)}'로 대신 사용하십시오. –
@AlexeyRomanov : 고마워요 - 옵션으로 추가 할 수도 있습니다 :) –
def timer (callback : => Unit) = {... 콜백; ...}, 그리고 타이머 (println (frenchDate)) (또는 타이머 {println (frenchDate)} 중 어느 것이 더 좋든간에) –