Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

scala - Function literal with multiple implicit arguments

How to define function literal with multiple implicit arguments in Scala? I've tried this way:

def create = authAction { (implicit request, user) ? // Syntax error
  Ok(html.user.create(registrationForm))
}

but it throws compilation error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As stated in previous answer, you can only define a single implicit parameter for a function literal, but there is workaround.

Instead of multiple implicit arguments you can write function literal as taking multiple argument lists with one argument each. Then it is possible to mark each argument as implicit. Rewriting original snippet:

def create = authAction { implicit request ? implicit user ?
  Ok(html.user.create(registrationForm))
}

You can call it from authAction as f(request)(user).

implicit keyword duplication is annoying, but at least it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...