Scala Code demystify

前端 未结 2 1179
说谎
说谎 2021-02-20 12:39

Could someone demystify this code which is part of the zentasks example in the Play20 framework. I\'m curious how this works, granted I\'m new to Scala from Java so a lot of thi

2条回答
  •  臣服心动
    2021-02-20 12:49

    First off a user guide to my answer: I will use italics to indicate a function that is used without being explicitly named (see anonymous functions).

    IsAuthenticated is a method which takes as a parameter an argument f.

    f is a function which takes Y as a parameter and produces an instance of Result

    Y is a function which takes Z as a parameter and produces an instance of Request[AnyContent]

    Z is a function which takes no parameters and returns a String

    IsAuthenticated calls Security.Authenticated, passing username and onUnauthorized (a function to call when the user is not authorized to perform the requested action).

    I am not entirely sure what's going on past here myself- I'm not quite that good with Scala yet- but my guess is that Security.Authenticated is a case class, and the following is equivalent to subclassing it and adding a constructor in java:

    {
      Action(request => f(user)(request))
    }
    

    If that much of my assumption is correct, then Action (which is a method on Security.Authenticated) is being called, passing as an argument a A.

    A is a function which takes a Request object (I am guessing at this class name) and produces a Result. The use of Result is implied here because the implementation of A is a call to f.

    So when the subclass of Security.Authenticated is instanciated, Action is called, which authenticates the user for some action (specified as a String) and then if the user is authenticated, returns f (the original parameter) which is presumably called by Action (after the aforementioned authentication). This call to f returns a Result, which is also a function. Result is then finally called with request (which was passed to A) as a parameter.

提交回复
热议问题