[英]Compilation error using Scalaz Functor for 7-arity function
I try to implement this answer and I encounter a compilation error : 我尝试实现此答案 ,但遇到编译错误:
def $match(o: DBObject) = MongoDBObject("$match" -> o)
def getObj(instance : String, contextValue: Option[SpaceId] = None,
eventType : Option[String] = None,
sourceValue: Option[Long] = None, targetValues: Option[Iterable[Long]] = None,
startDate : Option[LocalDate] = None, endDate : Option[LocalDate] = None) : DBObject = {
…
}
def getMatchObj = (getObj _).map($match)
And the error is : 错误是:
[info] Compiling 1 Scala source to /mnt/data/backup/dev/projects/bluekarma/bluekarma- analytics-scala-play2/target/scala-2.10/classes...
[error] /mnt/data/backup/dev/projects/bluekarma/bluekarma-analytics-scala-play2/app/models/Dao.scala:121: Unable to unapply type `(String, Option[modelsIds.SpaceId], Option[String], Option[Long], Option[Iterable[Long]], Option[org.joda.time.LocalDate], Option[org.joda.time.LocalDate]) => com.mongodb.casbah.query.Imports.DBObject` into a type constructor of kind `M[_]` that is classified by the type class `scalaz.Functor`
[error] 1) Check that the type class is defined by compiling `implicitly[scalaz.Functor[<type constructor>]]`.
[error] 2) Review the implicits in object Unapply, which only cover common type 'shapes'
[error] (implicit not found: scalaz.Unapply[scalaz.Functor, (String, Option[modelsIds.SpaceId], Option[String], Option[Long], Option[Iterable[Long]], Option[org.joda.time.LocalDate], Option[org.joda.time.LocalDate]) => com.mongodb.casbah.query.Imports.DBObject])
[error] def getMatchObj = (getObj _).map($match)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 3 s, completed Feb 6, 2014 9:53:13 AM
I guess it's a problem with scala compiler: it can't use Function7[...., T]
as M[T]
. 我猜这是Scala编译器的问题:它不能使用Function7[...., T]
作为M[T]
。
You could help to compiler this way: 您可以通过以下方式帮助编译:
type MF7[T] = (String, Option[SpaceId], Option[String], Option[Long], Option[Iterable[Long]], Option[LocalDate], Option[LocalDate]) => T
(getObj _: MF7[DBObject]).map(myMatch)
Note that you should avoid $
in names. 请注意,应避免在名称中使用$
。 $
is widely used in scala for generated names. $
在scala中广泛用于生成名称。
map
works for Function6
, so you could just reduce parameters cont. map
适用于Function6
,因此您可以减少参数cont。 For instance you could group parameters using case class
: 例如,您可以使用case class
参数进行分组:
case class Period(startDate: Option[LocalDate] = None, endDate: Option[LocalDate] = None)
def getObj(instance: String, contextValue: Option[SpaceId] = None,
eventType: Option[String] = None, sourceValue: Option[Long] = None,
targetValues: Option[Iterable[Long]] = None,
period: Option[Period] = None) : DBObject = ???
val getMatchObj = (getObj _).map(myMatch)
With shapeless
you could convert function of any arity to function of single HList
argument and vice versa: 使用shapeless
您可以将任何变量的函数转换为单个HList
参数的函数,反之亦然:
val getMatchObj = (getObj _).toProduct.andThen(myMatch).fromProduct
scala
functions can't have default parameters, so the only way to preserve default parameters is to extract all parameters to case class
like this: scala
函数不能具有默认参数,因此保留默认参数的唯一方法是将所有参数提取到case class
如下所示:
case class GetObjArgs(instance: String, contextValue: Option[SpaceId] = None,
eventType: Option[String] = None, sourceValue: Option[Long] = None,
targetValues: Option[Iterable[Long]] = None,
startDate: Option[LocalDate] = None, endDate: Option[LocalDate] = None)
def getObj(a: GetObjArgs): DBObject = ???
val getMatchObj = (getObj _) andThen myMatch
getMatchObj(GetObjArgs("inst", sourceValue = Some(1)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.