简体   繁体   English

Scala 多态回调类型不匹配

[英]Scala polymorphic callback type mismatch

I'm sorry i couldn't find a better title.很抱歉我找不到更好的标题。

I'm trying to achieve something like the following我正在尝试实现以下目标

abstract class Person 

case class User(uid: String, firstname: String, active: String) extends Person
case class Admin(id: String, pseudo: String, securityClearance: String) extends Person

def innerFunctionForUser(user: User): List[String] = {
  List() :+ user.uid :+ user.firstname :+ user.active
}

def innerFunctionForAdmin(admin: Admin): List[String] = {
  List() :+ admin.id :+ admin.psuedo :+ admin.securityClearance
}

def outerFunction(person: Person, innerFunction: (Person) => List[String]): List[String] = {
  innerFunction(person)
}

so i could use it like that所以我可以这样使用它

val myUser = User("0c60c5b4-306d-4372-b60d-fd699c80e408", "joe", "false")
val myAdmin = Admin("178789", "jack", "high")

outerFunction(myUser, innerFunctionForUser)
outerFunction(myAdmin, innerFunctionForAdmin)

which does not type check不输入检查

type mismatch;
 found   : User => List[String]
 required: Person => List[String]

and i can't have the innerFunction accept a type person like this而且我不能让 innerFunction 接受这样的类型的人

def innerFunctionForUser(user: Person): List[String] = {
  List() :+ user.uid :+ user.firstname :+ user.active
}

I kept it simple here but i need case class with parameters of different type and different number of parameters.我在这里保持简单,但我需要具有不同类型和不同参数数量的参数的案例 class。 So i can't have them declared in the abstract class Person.所以我不能在抽象的 class Person 中声明它们。 Which would give哪个会给

value uid is not a member of Person

value firstname is not a member of Person

value active is not a member of Playground.Person

How can one make different case class with different parameters in types and numbers evaluate to the same type?如何使具有不同类型和数字参数的不同案例 class 评估为相同类型?

AND/OR和/或

How can one make aa callback polymorphic, kind a like this如何使 aa 回调多态,像这样

def outerFunction(person: Person, innerFunction: (SomeCaseClass) => List[String]): List[String] = {
  innerFunction(person)
}

Hope this is clear enough.希望这足够清楚。

Thanks for reading, Have a good One.感谢阅读,祝您生活愉快。

User and Admin are subtypes of Person but User => List[String] and Admin => List[String] are not subtypes of Person => List[String] . UserAdminPerson的子类型,但User => List[String]Admin => List[String]不是Person => List[String]子类型 User => List[String] and Admin => List[String] are actually supertypes of Person => List[String] . User => List[String]Admin => List[String]实际上是Person => List[String]超类型 Function type A => B is covariant with respect to B but contravariant with respect to A . Function 类型A => B相对于B协变的,但相对于A是逆变的。

Try to make outerFunction generic尝试使outerFunction泛型

def outerFunction[P <: Person](person: P, innerFunction: P => List[String]): List[String] = 
  innerFunction(person)

outerFunction(myUser, innerFunctionForUser) //List(0c60c5b4-306d-4372-b60d-fd699c80e408, joe, false)
outerFunction(myAdmin, innerFunctionForAdmin) //List(178789, jack, high)

You can also try to replace functions innerFunctionForUser , innerFunctionForAdmin with type class您也可以尝试用类型 class替换函数innerFunctionForUserinnerFunctionForAdmin

trait InnerFunction[P <: Person] {
  def apply(person: P): List[String]
}

object InnerFunction {
  implicit val forUser: InnerFunction[User] = 
    user => List(user.uid, user.firstname, user.active)
  implicit val forAdmin: InnerFunction[Admin] = 
    admin => List(admin.id, admin.pseudo, admin.securityClearance)
}

def outerFunction[P <: Person](person: P)(implicit innerFunction: InnerFunction[P]): List[String] = 
  innerFunction(person)

outerFunction(myUser) //List(0c60c5b4-306d-4372-b60d-fd699c80e408, joe, false)
outerFunction(myAdmin) //List(178789, jack, high)

Since type class InnerFunction acts on different data types now similarly (it produces list of values for all fields of a case class) you can even derive it:由于类型 class InnerFunction现在类似地作用于不同的数据类型(它为案例类的所有字段生成值列表),您甚至可以派生它:

trait InnerFunction[T] {
  def apply(t: T): List[String]
}

object InnerFunction {
  implicit def mkInnerFunction[T <: Product]: InnerFunction[T] =
    _.productIterator.map(_.asInstanceOf[String]).toList
}

def outerFunction[T](t: T)(implicit innerFunction: InnerFunction[T]): List[String] = 
  innerFunction(t)

      //or simply
// def outerFunction[T <: Product](t: T): List[String] =
//   t.productIterator.map(_.asInstanceOf[String]).toList
      //or
// def outerFunction(t: Product): List[String] =
//   t.productIterator.map(_.asInstanceOf[String]).toList

outerFunction(myUser) //List(0c60c5b4-306d-4372-b60d-fd699c80e408, joe, false)
outerFunction(myAdmin) //List(178789, jack, high)

(this will fail at runtime if not all fields of T are String s) or (如果不是T的所有字段都是String ,这将在运行时失败)

import shapeless.ops.hlist.ToList
import shapeless.{Generic, HList}

trait InnerFunction[T] {
  def apply(t: T): List[String]
}

object InnerFunction {
  implicit def mkInnerFunction[T <: Product, L <: HList](implicit
    generic: Generic.Aux[T, L],
    toList: ToList[L, String]
  ): InnerFunction[T] = generic.to(_).toList
}

def outerFunction[T](t: T)(implicit innerFunction: InnerFunction[T]): List[String] = 
  innerFunction(t)

      //or simply
// def outerFunction[T, L <: HList](t: T)(implicit
//   generic: Generic.Aux[T, L],
//   toList: ToList[L, String]
// ): List[String] = generic.to(t).toList

outerFunction(myUser) //List(0c60c5b4-306d-4372-b60d-fd699c80e408, joe, false)
outerFunction(myAdmin) //List(178789, jack, high)

(this will guarantee at compile time that all fields of T are String s). (这将保证在编译时T的所有字段都是String )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM