Scala:函数返回未知类型 [英] Scala: Function returning an unknown type

查看:49
本文介绍了Scala:函数返回未知类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想创建一个添加两种不同类型值的 add 方法,例如:

If I wanted to make an add method that adds values of two different types like:

add[T,S](op1: T, op2: S): ? = ...

我能想到的最显着的例子是基本的数字类型.如果我添加一个 Byte 和一个 Int 那么它会返回一个 Int.如果添加两个字节,它可能会返回一个 Int,具体取决于该字节的 -127 到 128 限制是否被打破.

The most notable example I can think of are the basic number types. If I add a Byte and an Int then it would return an Int. If two Bytes are added it could return an Int depending on if the -127 to 128 limit for the Byte is broken.

更进一步,如果我想创建具有相同特征的类,我希望它也这样做.

Further more if I wanted to create classes that had the same characteristics I would like it to do the same.

一种可能的解决方案是让类扩展相同的特征或类.但是对于 Scala 原始类型的示例,这并不适用,因为 Int、Double、Float、Byte 除了 Any 之外没有共享一个共同的祖先.

One possible solution is to have classes extend the same trait or class. But for the example of Scala primitive types this doesn't apply cause Int, Double, Float, Byte don't share a common ancestor besides Any.

我还查看了 Numeric[T] 特性,但在添加不同的原始类型时似乎没有帮助.

I've also looked at the Numeric[T] trait but that doesn't seem to help when adding different primitive types.

谢谢凯

推荐答案

这是某种联合类型类的典型例子.

This is a prime example for a some kind of joint-typeclass.

首先定义一个类似于 Numeric[T] 的东西,但为了可添加性:

Start by defining a thing just like Numeric[T], but for addability:

trait Addable[T, S] {
  type Result
  def add(x: T, y: S): Result
}

现在,定义您的 add 函数:

Now, define your add function:

def add[T,S](op1: T, op2: S)(implicit op: Addable[T, S]): op.Result =
  op.add(op1, op2)

剩下要做的就是为Addable创建隐式实例:

All you have to do left, is to create implicit instances for Addable:

// Put them in the companion object, so the implicits are available
// without explicit import
object Addable {
  // Make any numeric addable
  implicit def numericAddable[T : Numeric]: Addable[T, T] = {
    new Addable[T, T] {
      type Result = T
      def add(x: T, y: T): T = {
        val op = implicitly[Numeric[T]]
        op.plus(x, y)
      }
    }
  }
}

但您现在也可以定义自己的类并定义(非对称)添加功能:

But you now can also define your own classes and define (assymetric) addition capability:

case class A(x: Int)
case class B(x: Int)
case class C(x: Int)

implicit object AddABC extends Addable[A,B] {
  type Result = C
  def add(x: A, y: B): C = C(x.x + y.x)
}

这将允许你写:

add(A(1), B(2))

但是,其中任何一个都会在编译时失败:

However, any of these will fail at compile time:

add(A(1), A(2))
add(B(1), A(2))
add(B(1), B(2))

不幸的是,这不适用于弱符合数字类型:

Unfortunately, this does not work for weak conforming numeric types:

add(1, 1) // compiles
add(1.0, 1.0) // compiles
add(1, 1.0) // fails to compile

另一篇文章来看,除了手动定义案例(当然还有一些辅助方法)之外,没有其他方法可以实现这一点.

Judging from another post, there is not really another way to achieve that, than to define the cases manually (with some helper methods of course).

这篇关于Scala:函数返回未知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆