我目前正在将应用程序的一部分移植到scala,它使用Oval库.方法问题是Validator.validate方法.它有两个 签名:
List<ConstraintViolation> validate(Object validatedObject)
List<ConstraintViolation> validate(Object validatedObject, String... profiles)
Run Code Online (Sandbox Code Playgroud)
scala代码看起来通常如下:
def validate(toValidate: AnyRef) = {
val validator = createValidator
validator.validate(toValidate)
}
Run Code Online (Sandbox Code Playgroud)
并且错误消息:
error: ambiguous reference to overloaded definition,
[INFO] both method validate in class Validator of type (x$1: Any,x$2: <repeated...>[java.lang.String])java.util.List[net.sf.oval.ConstraintViolation]
[INFO] and method validate in class Validator of type (x$1: Any)java.util.List[net.sf.oval.ConstraintViolation]
[INFO] match argument types (AnyRef)
[INFO] this.validator.validate(toValidate)
Run Code Online (Sandbox Code Playgroud)
我怎样才能明白这一点?
我有两个功能.
def process(date: DateTime, invoice: Invoice, user: User, reference: Reference) : (Action, Iterable[Billable])
def applyDiscount(billable: Billable) : Billable
Run Code Online (Sandbox Code Playgroud)
我如何编写这些以便我有一个函数(DateTime,Invoice,User,Reference)=>(Action,Iterable [Billable])
这是我想要的穷人方式
def buildFromInvoice(user: User, order: Invoice, placementDate: DateTime, reference: Reference) = {
val ab = billableBuilder.fromInvoice(user, order, placementDate, reference)
(ab._1, ab._2.map(applyDiscount(_))
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些代码,以便轻松链接返回Scalaz Validation
类型的函数.我试图编写的一种方法类似于Validation.flatMap
(我要调用的短路验证)andPipe
.另一个类似于|@|
on ApplicativeBuilder
(累积错误),除了它只返回Success
我将调用的最终类型andPass
假设我有功能:
def allDigits: (String) => ValidationNEL[String, String]
def maxSizeOfTen: (String) => ValidationNEL[String, String]
def toInt: (String) => ValidationNEL[String, Int]
Run Code Online (Sandbox Code Playgroud)
作为一个例子,我想首先将输入String传递给allDigits和maxSizeOf10.如果存在故障,则应通过不调用toInt函数来短路,并返回发生的一个或两个故障.如果成功,我想将Success值传递给toInt函数.从那里,它将成功输出值为Int,或者它将无法仅从toInt返回验证失败.
def intInput: (String) => ValidationNEL[String,Int] = (allDigits andPass maxSizeOfTen) andPipe toInt
Run Code Online (Sandbox Code Playgroud)
如果没有下面的附加实现,有没有办法做到这一点?
这是我的实施:
trait ValidationFuncPimp[E,A,B] {
val f: (A) => Validation[E, B]
/** If this validation passes, pass to f2, otherwise fail without accumulating. */
def andPipe[C](f2: (B) => Validation[E,C]): (A) => Validation[E,C] = (a: A) …
Run Code Online (Sandbox Code Playgroud) 与hibernate代理匹配的Scala模式不适用于继承对象的列表.要解决这个问题,我在案例类中包装了hibernate对象,请参阅http://oletraveler.com/2011/04/20/20/
我想要完成的是如果有人试图匹配继承的hibernate实体,则抛出编译时错误(可优先)或运行时错误.
例如:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
class PaymentSource
@Entity
class CreditCard
@Entity User {
var paymentSources: java.util.ArrayList
}
user.paymentSources.map(_ match {
case cc: CreditCard => println("oops") // <- this should error
})
Run Code Online (Sandbox Code Playgroud)
我尝试在CreditCard上覆盖unapply,但是这不起作用,因为只有在解构对象时调用unnapply,而不仅仅是在实例上进行匹配.
有没有?
我正在尝试创建一个函数,它采用高级类型的元组并将函数应用于高级类型中的类型。
在下面的例子中,有一个trait Get[A]
是我们的高级类型。还有一个 Get's: 元组(Get[String],Get[Int])
以及来自(String,Int) => Person
.
Scala-3 有一个名为 InverseMap 的匹配类型,它将类型 (Get[String], Get[Int]) 转换为本质上的类型 (String,Int)。
所以最终目标是编写一个函数,它可以接受一个任意数量Get[_]
类型的元组和一个输入与 InserveMap 类型匹配的函数,并最终返回 a Get[_]
,其中包装的类型是函数的结果。
我试图创建一个genericF
在下面调用的函数来显示所需的行为,尽管它可能不正确——但我认为它至少显示了正确的意图。
case class Person(name: String, age: Int)
trait Get[A] {
def get: A
}
case class Put[A](get: A) extends Get[A]
val t: (Get[String], Get[Int]) = (Put("Bob"), Put(42))
val fPerson: (String,Int) => Person = Person.apply _
def genericF[T<:Tuple,I<:Tuple.InverseMap[T,Get],B](f: I => B, t: T): Get[B] = ???
val person: Get[Person] = …
Run Code Online (Sandbox Code Playgroud)