Scala polymorphic callback type mismatch

我与影子孤独终老i 提交于 2020-07-22 06:08:11

问题


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

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. So i can't have them declared in the abstract 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 ?

AND/OR

How can one make a a callback polymorphic, kind a like this

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

Hope this is clear enough.

Thanks for reading, Have a good One.


回答1:


User and Admin are subtypes of Person but User => List[String] and Admin => List[String] are not subtypes of Person => List[String]. User => List[String] and Admin => List[String] are actually supertypes of Person => List[String]. Function type A => B is covariant with respect to B but contravariant with respect to A.

Try to make outerFunction generic

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

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:

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 Strings) or

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 Strings).



来源:https://stackoverflow.com/questions/62660986/scala-polymorphic-callback-type-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!