print(a+" ")) b..." />

Scala type inference with _ place holder?

The problem is that this List("This","is","Scala"). Foreach(print(_+" ")) is not equivalent to List("This","is","Scala"). Foreach(a => print(a+" ")) but to List("This","is","Scala").

Foreach(print(a => a+" ")) Now, let's see the type signature of foreach : def foreach B (f: (A) ⇒ B) : Unit where A is the type parameter of the List itself. Since we have a ListString the compiler knows one has to pass to foreach a FunctionString, B In a => print(a+" ") the type of a is already known then: String In print(a => a+" ") there is a problem, as print is not a Function However, the compiler hasn't considered that yet -- it's still trying to compile a => a So let's look at the type of Predef. Print : def print (x: Any) : Unit So a => a must be of type Any which, of course, means it can be anything.It doesn't help the compiler in asserting what the type of a is.

Which doesn't really matter, because you didn't want to print a Function in first place.

The problem is that this List("This","is","Scala"). Foreach(print(_+" ")) is not equivalent to List("This","is","Scala"). Foreach(a => print(a+" ")) but to List("This","is","Scala").

Foreach(print(a => a+" ")) Now, let's see the type signature of foreach: def foreach B (f: (A) ⇒ B) : Unit where A is the type parameter of the List itself. Since we have a ListString, the compiler knows one has to pass to foreach a FunctionString, B. In a => print(a+" ") the type of a is already known then: String.In print(a => a+" ") there is a problem, as print is not a Function.

However, the compiler hasn't considered that yet -- it's still trying to compile a => a+" ". So let's look at the type of Predef. Print: def print (x: Any) : Unit So a => a+" " must be of type Any, which, of course, means it can be anything.

It doesn't help the compiler in asserting what the type of a is. Which doesn't really matter, because you didn't want to print a Function in first place.

Scala reads (_+" ") as x => x+" ". But println doesn't contain any type information to help the compiler guess what type x might be, so it gives that error. What you wanted it to do was notice that println didn't work and recurse backwards and try it again for foreach.

But it can't do that: println can take any argument, including a function, so x => x+" " is a perfectly valid thing for println to try to print. (And even in a case where it could in theory backtrack, it generally doesn't. ).

Where A is the type parameter of the List itself. Since we have a ListString, the compiler knows one has to pass to foreach a FunctionString, B. In a => print(a+" ") the type of a is already known then: String.

In print(a => a+" ") there is a problem, as print is not a Function. However, the compiler hasn't considered that yet -- it's still trying to compile a => a+" ". So let's look at the type of Predef.

So a => a+" " must be of type Any, which, of course, means it can be anything. It doesn't help the compiler in asserting what the type of a is. Which doesn't really matter, because you didn't want to print a Function in first place.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions