scala loan pattern, optional function param

后端 未结 1 1052
终归单人心
终归单人心 2021-02-04 19:43

I have a loan pattern that applies a function n times where \'i\' is the incrementing variable. \"Occasionally\", I want the function passed in to have access to \'i\'....but I

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 20:33

    def withLoaner(n: Int) = new {
      def apply(op: Int => String) : String = (1 to n).map(op).mkString("\n")
      def apply(op: () => String) : String = apply{i: Int => op()}
    }
    

    (not sure how it is related to the loan pattern)

    Edit Little explanation as requested in comment.

    Not sure what you know and don't know of scala and what you don't undestand in that code. so sorry if what I just belabor the obvious.

    First, a scala program consist of traits/classes (also singleton object) and methods. Everything that is done is done by methods (leaving constructor aside). Functions (as opposed to methods) are instances of (subtypes of) the various FunctionN traits (N the number of arguments). Each of them has as apply method that is the actual implemention. If you write

    val inc = {i: Int => i + 1}
    

    it is desugared to

    val inc = new Function1[Int, Int] {def apply(i: Int) = i + 1}
    

    (defines an anonymous class extending Function1, with given apply method and creating an instance)

    So writing a function has rather more weight than a simple method. Also you cannot have overloading (several methods with the same name, differing by the signature, just what I did above), nor use named arguments, or default value for arguments.

    On the other hand, functions are first classes values (they can be passed as arguments, returned as result) while methods are not. They are automatically converted to functions when needed, however there may be some edges cases when doing that. If a method is intended solely to be used as a function value, rather than called as a method, it might be better to write a function.

    A function f, with its apply method, is called with f(x) rather than f.apply(x) (which works too), because scala desugars function call notation on a value (value followed by parentheses and 0 or more args) to a call to method apply. f(x) is syntactic sugar for f.apply(x). This works whatever the type of f, it does not need to be one of the FunctionN.

    What is done in withLoaner is returning an object (of an anonymous type, but one could have defined a class separately and returned an instance of it). The object has two apply methods, one accepting an Int => String, the other one an () => String. When you do withLoaner(n)(f) it means withLoaner(n).apply(f). The appropriate apply method is selected, if f has the proper type for one of them, otherwise, compile error.

    Just in case you wonder withLoaner(n) does not mean withLoaner.apply(n) (or it would never stop, that could just as well mean withLoaner.apply.apply(n)), as withLoaner is a method, not a value.

    0 讨论(0)
提交回复
热议问题