scala-placeholder-syntax

Scala underscore - ERROR: missing parameter type for expanded function

只愿长相守 提交于 2019-12-27 13:59:22
问题 I know there have been quite a few questions on this, but I've created a simple example that I thought should work,but still does not and I'm not sure I understand why val myStrings = new Array[String](3) // do some string initialization // this works myStrings.foreach(println(_)) // ERROR: missing parameter type for expanded function myStrings.foreach(println(_.toString)) Can someone explain why the second statement does not compile? 回答1: It expands to: myStrings.foreach(println(x => x

Scala underscore - ERROR: missing parameter type for expanded function

半城伤御伤魂 提交于 2019-12-27 13:59:00
问题 I know there have been quite a few questions on this, but I've created a simple example that I thought should work,but still does not and I'm not sure I understand why val myStrings = new Array[String](3) // do some string initialization // this works myStrings.foreach(println(_)) // ERROR: missing parameter type for expanded function myStrings.foreach(println(_.toString)) Can someone explain why the second statement does not compile? 回答1: It expands to: myStrings.foreach(println(x => x

Scala underscore use to simplify syntax of function literals

可紊 提交于 2019-12-23 10:28:24
问题 I have the following code: var x = Array(1,3,4,4,1,1,3) var m = Int.MaxValue x.foreach((x)=>(m = m min x)) I tried to simplify last sentence to: x.foreach((m = _ min m)) But the interpreter says: scala> x.foreach((m = _ min m)) <console>:8: error: missing parameter type for expanded function ((x$1) => x$1.min(m)) x.foreach((m = _ min m)) ^ I tried to be more explicit about the type: scala> x.foreach((m = (_:Int) min m)) <console>:8: error: type mismatch; found : (Int) => Int required: Int x

Scala underscore use to simplify syntax of function literals

可紊 提交于 2019-12-23 10:26:23
问题 I have the following code: var x = Array(1,3,4,4,1,1,3) var m = Int.MaxValue x.foreach((x)=>(m = m min x)) I tried to simplify last sentence to: x.foreach((m = _ min m)) But the interpreter says: scala> x.foreach((m = _ min m)) <console>:8: error: missing parameter type for expanded function ((x$1) => x$1.min(m)) x.foreach((m = _ min m)) ^ I tried to be more explicit about the type: scala> x.foreach((m = (_:Int) min m)) <console>:8: error: type mismatch; found : (Int) => Int required: Int x

Underscores and string concatenation in List.map with Scala [duplicate]

≯℡__Kan透↙ 提交于 2019-12-19 11:50:23
问题 This question already has answers here : Scala foreach strange behaviour (5 answers) Closed 5 years ago . Scala lets you use an underscore to do a simple map. So for example instead of writing: def roleCall(people: String*){ people.toList.map(x => println(x)) } ...I can instead write: def roleCall(people: String*){ people.toList.map(println(_)) } However for some reason I can't write: def greet(people: String*){ // This won't compile! people.toList.map(println("Hello " + _)) } instead I have

Force grouping of ascription on underscore in scala

六月ゝ 毕业季﹏ 提交于 2019-12-11 15:39:25
问题 I am trying to do: MyObject.myMethod(_:MyType.myAttribute) This fails with type myAttribute is not a member of object MyObject which is correct. The problem is that I want to call myMethod on myAttribute of _:MyType , not ascribe MyType:myAttribute to _ . Can I somehow group the type ascription _:MyType ? (_:MyType).myAttribute returns type MyType => classOf(myAttribute) , which is not what I want. Edit: I changed the title and text of this post to no longer refer to this as the associativity

what is does it mean println(_)?

帅比萌擦擦* 提交于 2019-12-11 11:23:56
问题 I have this piece of code in scala val wordCounts = logData.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b) wordCounts.foreach(println(_)) So what does println(_) mean and what should it print? 回答1: As explained in the section "Placeholder Syntax for Anonymous Functions" of the Spec, println(_) is a shortcut for the anonymous function literal w => println(w) which in turn is a shortcut for something like (w: (String, Int)) => println(w) in this particular

Underscores and string concatenation in List.map with Scala [duplicate]

有些话、适合烂在心里 提交于 2019-12-01 12:43:30
This question already has an answer here: Scala foreach strange behaviour 5 answers Scala lets you use an underscore to do a simple map. So for example instead of writing: def roleCall(people: String*){ people.toList.map(x => println(x)) } ...I can instead write: def roleCall(people: String*){ people.toList.map(println(_)) } However for some reason I can't write: def greet(people: String*){ // This won't compile! people.toList.map(println("Hello " + _)) } instead I have to write: def greet(people: String*){ people.toList.map(x => println("Hello " + x)) } Can anyone explain why? Basically, the

What are the rules to govern underscore to define anonymous function?

风流意气都作罢 提交于 2019-11-28 10:11:03
I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want. List(1,2,3) foreach println(_:Int) //error ! List(1,2,3) foreach (println(_:Int)) //work List(1,2,3) foreach(println(_:Int)) //work Using -Xprint:typer I can see Scala transforms the first one into "a big anonymous function": x$1 => List(1,2,3) foreach(println(x$1:Int)) the worked 2th 3th are right transformation into what I want. ... foreach (x$1 => println(x$1:Int)) Why

What are the rules to govern underscore to define anonymous function?

霸气de小男生 提交于 2019-11-27 03:27:38
问题 I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want. List(1,2,3) foreach println(_:Int) //error ! List(1,2,3) foreach (println(_:Int)) //work List(1,2,3) foreach(println(_:Int)) //work Using -Xprint:typer I can see Scala transforms the first one into "a big anonymous function": x$1 => List(1,2,3) foreach(println(x$1:Int)) the