如何在 scala 中返回通用特征的子类型?

发布于 2025-01-10 00:51:01 字数 703 浏览 2 评论 0原文

我正在尝试创建一个工厂模式,返回类型是一个参数化特征,实际返回类型将是该特征的子类型,但我事先不知道泛型类型,所以我不知道如何指定返回类型。简化版本是这样的:

trait Mapper[T] {
    def createMap(a: T, b: T): T
}

class MapperA extends Mapper[String]{
    override def createMap(a: String, b: String): String = {
        a + b
    }
}

class MapperB extends Mapper[Int]{
    override def createMap(a: Int, b: Int): Int = {
        a + b + b + b
    }
}

def factory(typeOfMapper: String): Mapper = {
    typeOfMapper match {
        case "mapperA" => new MapperA()
        case "mapperB" => new MapperB()
    }
}

val mapper = factory("mapperA")

这会给我和错误

特征映射器采用类型参数

,但我事先不知道泛型类型。这里的工厂方法的返回类型应该是什么?

I am trying to create a factory pattern, and the return type is a parametrized trait, and the actual return type will be a subtype of that trait, but I do not know the generic type beforehand, so I don't know how to specify the return type. A simplified version is like this:

trait Mapper[T] {
    def createMap(a: T, b: T): T
}

class MapperA extends Mapper[String]{
    override def createMap(a: String, b: String): String = {
        a + b
    }
}

class MapperB extends Mapper[Int]{
    override def createMap(a: Int, b: Int): Int = {
        a + b + b + b
    }
}

def factory(typeOfMapper: String): Mapper = {
    typeOfMapper match {
        case "mapperA" => new MapperA()
        case "mapperB" => new MapperB()
    }
}

val mapper = factory("mapperA")

This will give me and error of

trait Mapper takes type parameters

but I do not know the generic type beforehand. What should the return type be for the factory method here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2025-01-17 00:51:01

好吧,您可以返回 Mapper[_] ...这将编译,但并不是非常有用,正如评论中指出的:您将无法在任何有意义的方式,因为 create 的实际类型将是未知的。

如果映射器的不同实例始终具有不同的类型参数(至少在同一范围内),那么一个巧妙的解决方案是使用隐式:

object Mapper { 
   implicit def strMapper: Mapper[String] = new MapperA
   implicit def intMapper: Mapper[Int] = new MapperB
}

然后在任何可见的地方,您都可以这样做:

      val m1: Mapper[String] = implicitly[Mapper[String]] 
      val m2: Mapper[Int] = implicitly[Mapper[Int]]

您还可以编写工厂函数(不过,我不确定你为什么想要)像这样:

    def mapperFactory[T: Mapper] = implicitly[Mapper[T]]

并像这样使用它:

     val m: Mapper[String] = mapperFactory

或者像这样

    def intMapper = mapperFactory[Int]   

如果你想要相同类型参数的不同映射器,它基本上是相同的想法,除了它看起来不像整洁无隐含。关键是不同类型的工厂不同:

   class Mapper {
       def str(`type`: String): Mapper[String] = `type` match { 
         case "foo" => FooMapper()
         case "bar" => BarMapper()
       }
       def int(`type`: String): Mapper[Int] = `type` match {
          case "baz" => BazMapper()
          case "bak" => BakMapper()
       }
   }

   val fooMapper = Mapper.str("foo")
   val bakMapper = Mapper.int("bak")

Well, you could return Mapper[_] ... that will compile, but isn't really very useful, as pointed out in the comments: you won't be able to use the returned value in any meaningful way, because actual type of create will be unknown.

If different instances of your mapper will always have different type parameters (at least, within the same scope), then a neat solution would be using implicits:

object Mapper { 
   implicit def strMapper: Mapper[String] = new MapperA
   implicit def intMapper: Mapper[Int] = new MapperB
}

Then everywhere you have these visible, you can just do:

      val m1: Mapper[String] = implicitly[Mapper[String]] 
      val m2: Mapper[Int] = implicitly[Mapper[Int]]

You could also write your factory function (though, I am not sure why you'd want to) like this:

    def mapperFactory[T: Mapper] = implicitly[Mapper[T]]

and use it like this:

     val m: Mapper[String] = mapperFactory

or like this

    def intMapper = mapperFactory[Int]   

If you want different mappers for the same type parameter, it's basically the same idea, except it does't look as neat without implicits. The key is different factories for different types:

   class Mapper {
       def str(`type`: String): Mapper[String] = `type` match { 
         case "foo" => FooMapper()
         case "bar" => BarMapper()
       }
       def int(`type`: String): Mapper[Int] = `type` match {
          case "baz" => BazMapper()
          case "bak" => BakMapper()
       }
   }

   val fooMapper = Mapper.str("foo")
   val bakMapper = Mapper.int("bak")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文