Target scala type (=> A) => O

I found a weird type in this code on github:

  final class StaticRouteB[Page, O](private val f: (=> Action[Page]) => O) extends AnyVal {
    def ~>(a: => Action[Page]): O = f(a)
  }
  • Type f. What does it mean? I assume this is a function with a call to the name argument. I've never seen this before, so I'm not sure what it is or how this thing works.

  • Is there a way to translate this type into something more "standard"? (I mean the code above, so that it does not use a type (=> Action[Page]) => O)? Is this type only syntactic sugar?)

  • I wonder what will happen if I go through Action[Page] => Oas fat creation StaticRouteB? Will I get a compilation error? Runtime Error? What for? I mean, what is the purpose (=> ... )? Is this a cause of a compilation error if the correct function type is not passed in or if the parameter evaluation strategy is changed f? I mean, why does anyone want to be of this type? For what purpose?

+4
source share
1 answer

(=> Action[Page]) is a call by name parameter.

So f: (=> Action[Page]) => Ois a function with a call to the name parameter of type Action [Page], and this function returns a result of type O.

You can see its use in the method definition ~>.

+7
source

Source: https://habr.com/ru/post/1663493/


All Articles