scala - Why this error : type mismatch; found : Unit required: String => Unit -
below code :
object gen { println("welcome scala worksheet") case class cs(funtorun: string => unit) val l: list[cs] = list( cs(open("filepath")) ) def open(path: string): unit = { java.awt.desktop.getdesktop.open(new java.io.file(path)) } }
causes compiler error :
type mismatch; found : unit required: string => unit
but open function of of type string => unit
, param type of funtorun string => unit
?
cs
accepts function string
unit
, you're passing result of open
, unit
.
if want pass open
function instead, need do:
cs(open)
although without knowing you're trying achieve, it's impossibile provide sensible solution.
a possible alternative performing open
action @ later time
case class cs(funtorun: () => unit) // function not taking parameters longer val cs = cs(() => open("filepath")) // 'open' not performed here cs.funtorun() // 'open' performed here
this way open
function not evaluated when passed cs
constructor.
Comments
Post a Comment