簡體   English   中英

Scala + Play框架:需要進行動作組成說明

[英]Scala + Play Framework: Action composition explanation needed

這是Play框架隨附的示例中的動作組成

def IsAuthenticated(f: => String => Request[AnyContent] => Result) = Security.Authenticated(username, onUnauthorized) { user =>
    Action(request => f(user)(request))
  }   

因此, Security.Authenticated使用username: RequestHeader => Option[String]onAuthorized: RequestHeader=>SimpleResult ,第二組括號采用String => Action[A]

然后在控制器中,我有:

def index = isAuthenticated { ...code }}  

上面的代碼是這個,所以我假設這是f函數=> String => Request[AnyContent] => Result 現在,我不明白的是這里真正發生的事情。 我不是在談論User.findByEmail.... ,我是在談論username => _ => ... 如果直接調用此函數,其簽名將是什么樣?

username => _ =>
    User.findByEmail(username).map { user =>
      Ok(
        html.dashboard(
          Project.findInvolving(username), 
          Task.findTodoInvolving(username), 
          user
        )
      )
    }.getOrElse(Forbidden)  

如果有def isAuthenticated(f: => Request[AnyContent] => Result)我會知道如何使用它並且我會理解。 但是多余的“數據”使我感到困惑。

更新:

我想我發現了一些東西:

def f2: String => Int => List[Char] = x => _ => x.toList  

這將被稱為:

f2("Andrew")(2) //there can be anything replacing 2 because I don't have access to it anyway  

所以上面我主要詢問的功能是:

def f: => String => Request[AnyContent] => Result = username => _ => User.find.....  

我對嗎?
我收到“此處不允許使用名稱參數錯誤”。

如果他們不使用第二個參數,為什么要使用String => Request => Result而不僅僅是String => Result

該函數定義實際上是咖喱函數定義。

String => Request => Result實際上意味着: f(s:String):(r:Request)=>Result即,接受字符串並返回接受請求並返回結果的函數

查閱“檢查功能”部分: http ://danielwestheide.com/blog/2013/01/30/the-neophytes-guide-to-scala-part-11-currying-and-partially-applied-functions .html

對我來說, https://github.com/mariussoutier/PlayBasics/tree/master/play-2.2-migration中的示例非常有啟發性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM