why scala placeholder doesn't work -
hello i'm trying understand symbol "_" in scala, looks wildcard did not understand why in given scenario.
var l = list("a","b" ,"c") // works "s" works variable. l.foreach( s => if(s=="a"){ print(s) } ) // works _ takes place of "s" l.foreach( print(_) ) //so doubt whether "_" wildcard not work well. l.foreach( if(_=="a"){ print(_) } )
"_" should act variable s
, why doesn't?
wildcards in anonymous functions expanded in way n-th _ treated n-th argument. way you're using makes scala compiler think you're have like
l.foreach((x,y) => if(x=="a"){ print(y) } )
which invalid.
Comments
Post a Comment