小编Kri*_*ala的帖子

可选参数值是否可能依赖于Scala中的另一个参数

有没有人知道Scala中是否有类似的东西:

case class Thing(property:String)

def f(thing:Thing, prop:String = thing.property) = println(prop)
Run Code Online (Sandbox Code Playgroud)

上面的代码没有编译; 给错误error: not found: value thingthing.property

以下显示了预期的行为:

f(Thing("abc"), "123") // prints "123"
f(Thing("abc"))        // prints "abc"
Run Code Online (Sandbox Code Playgroud)

我意识到我可以使prop参数成为一个Option[String]并在函数定义中进行检查,但我想知道是否有一种方法可以使用2.8.0中的新命名/默认参数支持.

scala default-value named-parameters scala-2.8

17
推荐指数
1
解决办法
2121
查看次数

有界类型参数化案例类和Scala中的默认参数的问题

请考虑以下内容(使用Scala 2.8.1和2.9.0测试):

trait Animal
class Dog extends Animal

case class AnimalsList[A <: Animal](list:List[A] = List())
case class AnimalsMap[A <: Animal](map:Map[String,A] = Map())

val dogList = AnimalsList[Dog]()  // Compiles
val dogMap = AnimalsMap[Dog]()    // Does not compile
Run Code Online (Sandbox Code Playgroud)

最后一行失败了:

error: type mismatch;
 found   : scala.collection.immutable.Map[Nothing,Nothing]
 required: Map[String,Main.Dog]
Note: Nothing <: String, but trait Map is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: String`. (SLS 3.2.10)
Error occurred in an application involving default arguments. …
Run Code Online (Sandbox Code Playgroud)

scala case-class default-arguments

7
推荐指数
2
解决办法
6050
查看次数

Cake Pattern可以用于非单例样式依赖吗?

我遇到的蛋糕模式的大多数示例似乎都将依赖关系视为单例类型服务; 在组件的最终组装中,每种类型只有一个实例.在使用Cake Pattern进行依赖注入时,是否可以编写具有多个特定类型实例的配置(可能以不同方式配置)?

请考虑以下组件.通用HTTP服务:

trait HttpService { def get(query:String):String }
trait HttpServiceComponent {
  val httpService:HttpService
  class HttpServiceImpl(address:String) extends HttpService {
    def get(query:String):String = ...
  }
}
Run Code Online (Sandbox Code Playgroud)

贸易和公司服务,每个服务都依赖于HttpService,它可能是不同的实例:

trait TradeService { def lastTrade(symbol:String):String }
trait TradeServiceComponent {
  this:HttpServiceComponent => // Depends on HttpService
  val tradeService:TradeService
  class TradeServiceImpl extends TradeService {
    def lastTrade(symbol:String):String =
      httpService.get("symbol=" + symbol)
  }
}

trait CompanyService { def getCompanySymbols(exchange:String):String }
trait CompanyServiceComponent {
  this:HttpServiceComponent =>  // Depends on different HttpService instance
  val companyService:CompanyService
  class CompanyServiceImpl extends CompanyService …
Run Code Online (Sandbox Code Playgroud)

dependency-injection scala robot-legs-problem

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

有没有更好的方法在Scala中提升PartialFunction?

我偶尔会遇到以下模式,我基本上有一个PartialFunction[SomeType,AnotherType],并希望将其视为一个Function[SomeType,Option[AnotherType],例如:

def f(s:SomeType):Option[AnotherType] = s match {
  case s1:SubType1 => Some(AnotherType(s1.whatever))
  case s2:SubType2 => Some(AnotherType(s2.whatever))
  case _ => None
}
Run Code Online (Sandbox Code Playgroud)

有没有办法以避免默认情况并将结果包装在Some定义位置的方式编写上述函数?到目前为止我提出的最好的是:

def f(s:SomeType):Option[AnotherType] = pf.lift(s)
def pf:PartialFunction[SomeType,AnotherType] = {
  case s1:SubType1 => AnotherType(s1.whatever)
  case s2:SubType2 => AnotherType(s2.whatever)
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有定义中间函数的情况下做到这一点?我已经尝试了以下几行,但还没有任何东西可以编译:

def f:Function[SomeType,Option[AnotherType]] = {
  case s1:SubType1 => AnotherType(s1.whatever)
  case s2:SubType2 => AnotherType(s2.whatever)
}.lift
Run Code Online (Sandbox Code Playgroud)

functional-programming scala partialfunction

3
推荐指数
1
解决办法
1001
查看次数