Cannot create apply function with static language?

前端 未结 12 871
梦谈多话
梦谈多话 2021-02-04 02:09

I have read that with a statically typed language like Scala or Haskell there is no way to create or provide a Lisp apply function:

(apply #\'+ (lis         


        
12条回答
  •  囚心锁ツ
    2021-02-04 02:53

    On this page, I read that "Apply is just like funcall, except that its final argument should be a list; the elements of that list are treated as if they were additional arguments to a funcall."

    In Scala, functions can have varargs (variadic arguments), like the newer versions of Java. You can convert a list (or any Iterable object) into more vararg parameters using the notation :_* Example:

    //The asterisk after the type signifies variadic arguments
    def someFunctionWithVarargs(varargs: Int*) = //blah blah blah...
    
    val list = List(1, 2, 3, 4)
    someFunctionWithVarargs(list:_*)
    //equivalent to
    someFunctionWithVarargs(1, 2, 3, 4)
    

    In fact, even Java can do this. Java varargs can be passed either as a sequence of arguments or as an array. All you'd have to do is convert your Java List to an array to do the same thing.

提交回复
热议问题