相关疑难解决方法(0)

含有隐含的Scala函数文字

请原谅我,如果已经在别处问过这个问题.我有一个涉及函数值和隐式参数的Scala语法问题.

我很自在地使用Scala的currying功能.例如,如果我有一个sum函数,并希望使第二个参数隐含:

scala> def sum(a: Int)(implicit b: Int) = a + b
sum: (a: Int)(implicit b: Int)Int
Run Code Online (Sandbox Code Playgroud)

有没有办法使用函数值语法执行此操作?忽略隐含片刻,我通常会写出如下的curried函数值:

scala> val sum2 = (a: Int) => (b: Int) => a + b
sum: (Int) => (Int) => Int = <function1>
Run Code Online (Sandbox Code Playgroud)

但是,第二种方法中的函数签名是非常不同的(currying正在明确表达).只是将隐式关键字添加到b中没有多大意义,编译器也会抱怨:

scala> val sum2 = (a: Int) => (implicit b: Int) => a + b
<console>:1: error: '=>' expected but ')' found.
       val sum2 = (a: Int) => (implicit b: Int) => a + b
                                              ^
Run Code Online (Sandbox Code Playgroud)

此外,从第一种获取函数值的方法中部分应用总和也会导致问题:

scala> val sumFunction = …
Run Code Online (Sandbox Code Playgroud)

scala function implicit currying literals

25
推荐指数
3
解决办法
4667
查看次数

play/scala,隐含请求=>是什么意思?

大多数播放框架我都看到了代码块

// Returns a tasks or an 'ItemNotFound' error
def info(id: Long) = SecuredApiAction { implicit request =>
  maybeItem(Task.findById(id))
}
Run Code Online (Sandbox Code Playgroud)

是的,我的理解是定义一个方法info(id: Long),在scala doc中用scala创建函数,语法如下:

def functionName ([list of parameters]) : [return type] = {
   function body
   return [expr]
}
Run Code Online (Sandbox Code Playgroud)

你能告诉我是什么意思implicit request =>,并SecuredApiAction把之前{

scala playframework

5
推荐指数
1
解决办法
2382
查看次数

标签 统计

scala ×2

currying ×1

function ×1

implicit ×1

literals ×1

playframework ×1