How do you write shortened version of function literal?

Could you please explain to me how to write shortened version of function literal ?

I'm going through this Spray tutorial in which following code

val route: Route = { ctx => ctx.complete("yeah") }

get compressed to

val route: Route = complete("yeah")

where complete is function of RouteDirectives.

I cannot reproduce the same in my code which is

class Test
  def testFunc(value : Int) {

type MyType = Test => Unit
val asd : MyType = _.testFunc(10)
asd(new Test)

If I write val asd : MyType = testFunc(10) instead I (obviously) get error

error: type mismatch;
found   : Int(10)
required: Int => Unit
val asd : MyType = testFunc(10)

So I thought maybe 'complete' is also an object with apply method. And indeed following works

val route: Route = complete.apply("yeah")

But I don't see an object. In the debugger it steps into RouteDirectives.complete as expected.

Why ? What am I missing ?


complete returns a function. When you call complete("yeah"), you are supplying the "yeah" parameter to that function (they call it the magnet).

So to do something similar you would write:

def testFunc: Int => Unit = (i: Int) => println(i)

Putting it together with calling a method on an object Test:

def testFunc: Int => (Test => Unit) = (i: Int) => {
  (test: Test) => println(i)

Because of the way => associates to the right and type inference, this could be rewritten:

def testFunc: Int => Test => Unit = i => test => println(i)

It is then either very confusing or natural (the parameters mirror the types exactly).

So when in the tutorial they say it is equivalent, the authors of the library go to some length to make it look like it is "equivalent" by returning functions, it is not something that is built-in in the Scala syntax.

Need Your Help

Issuing “ --oauth update .” with multiple accounts?

google-app-engine oauth multiple-accounts

I configured OAuth for one account I have, It works perfectly with Appengine app_ids I'm using for that account.

VisualSVN undelete with TortoiseSVN

svn tortoisesvn visualsvn-server

Using TortoiseSVN against VisualSVN I delete a source file that I should not have deleted. Now this isn't a train smash because I can get the file back from the daily backup. However I would like to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.