相关疑难解决方法(0)

在Scala中提升函数值的方法

Scala库是否为将给定类型的方法提升为函数值提供了任何支持?

例如,假设我想举起String.length.我可以写

val f: String => Int = _.length
Run Code Online (Sandbox Code Playgroud)

要么

val f = { s: String => s.length }
Run Code Online (Sandbox Code Playgroud)

但是,这种语法并不总是理想的(特别是在较大的表达式中).我想我正在寻找能够实现表达式的东西

Lift[String](_.length)
Lift[Option[Int]].lift(_.filter)
Run Code Online (Sandbox Code Playgroud)

我记得这样的事情:

class Lift[T] {                                                          
   def apply[R](f: T => R): T => R = f

   def lift[A, R](f: (T) => (A) => R): (T, A) => R = 
         f(_)(_) 
   def lift[A1, A2, R](f: (T) => (A1, A2) => R): (T, A1, A2) => R =
         f(_)(_,_)
   // ... etc. ...
}
object Lift {
   def apply[T] = …
Run Code Online (Sandbox Code Playgroud)

scala

8
推荐指数
2
解决办法
3986
查看次数

方法调用的默认类型

我想知道在Scala中调用类型参数化方法时是否可以使用默认类型.假设我在某处有以下方法:

def apply[A]( id: String )( implicit processor: Processor[A] ) =
  processor( data get id )
Run Code Online (Sandbox Code Playgroud)

我想AString,当编译器没有关于来推断哪种类型的提示.所以我可以重载我的定义:

def apply( id: String )( implicit processor: Processor[String] ) =
  processor( data get id )
Run Code Online (Sandbox Code Playgroud)

但是这两种方法在擦除后都会有相同的签名...有没有办法提供默认类型?

generics types scala type-parameter

4
推荐指数
1
解决办法
170
查看次数

标签 统计

scala ×2

generics ×1

type-parameter ×1

types ×1