简体   繁体   English

使用Scalaz Functor进行7种功能的编译错误

[英]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)

shapeless 无形的

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

Default parameters 默认参数

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.

相关问题 使用子类(cats / scalaz)使用Functor调用泛型函数 - Calling generic function with Functor using subclass (cats/scalaz) 使用函子(Scalaz7)提升采用隐式参数的函数 - Lifting a function which takes implicit parameter using functor (Scalaz7) Scalaz 7如何将Functor与Function1结合使用 - Scalaz 7 how to use Functor with Function1 在Scalaz中使用Free与非functor - Using Free with a non-functor in Scalaz Scalaz Functor类型类特殊符号 - Scalaz Functor typeclass special symbols 为什么Scalaz中没有Array的Functor实例 - Why is there no Functor instance for Array in Scalaz 使用applicative functor进行Scalaz验证| @ | 不工作 - Scalaz Validation with applicative functor |@| not working 在使用类型投影后甚至在声明Functor之后编译错误 - Compilation error when declaring Functor for Either even after using type projections 在Set而不是List上使用scalaz的MA方法的编译问题 - Compilation issues using scalaz's MA methods on Set but not List 在 scala 无形库中,当 arity &gt; 22(可能使用无形宏之一)时,是否可以编写通用的 arity function? - In scala shapeless library, is it possible to write a generic arity function when the arity > 22 (presumably using one of shapeless macros)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM