如何将 Scala 转换导出到 Java

How to export Scala Transformation to Java

我正在尝试通过 Java 实现 Scala 特征,该特征在另一个容器中有一个通用容器类型,无法通过 java import 自动解决,Scala 代码如下:

import cats.data.{EitherNel, Kleisli, NonEmptyList}
import cats.implicits._

package Export_To_Java {
    package object types {
        type Valid[A] = EitherNel[String, A]
        type ValidOperation[A, B] = Kleisli[Valid, A, B]
        type Amount = BigDecimal
    }

    trait InterestService[Account] {
        import types._

        type InterestOperation = ValidOperation[Account, Option[Amount]]
        type TaxOperation = ValidOperation[Option[Amount], Amount]

        def computeInterest: InterestOperation
        def computeTax: TaxOperation
    }
}

一旦被Java实现,Intellij自动生成方法:

import cats.data.Kleisli;
import cats.data.NonEmptyList;
import Export_To_Java.InterestService;
import scala.math.BigDecimal;
import scala.util.Either;


public class JavaInterestService<Account> implements InterestService<Account> {
    @Override
    public Kleisli<Either<NonEmptyList<String>, A>, Account, Option<BigDecimal>> computeInterest() {
        throw new NotImplementedException();
    }

    @Override
    public Kleisli<Either<NonEmptyList<String>, A>, Option<BigDecimal>, BigDecimal> computeTax() {
        throw new NotImplementedException();
    }
}

现在你会看到 java 方法 return 类型已经扩展到 Either<NonEmptyList<String>, A> 里面仍然有一个泛型参数 A ,它是从 Scala 类型携带的Valid[A]。错误信息是:

Error:(13, 82) java: computeInterest() in JavaInterestService cannot implement computeInterest() in Export_To_Java.InterestService
  return type cats.data.Kleisli<scala.util.Either<cats.data.NonEmptyList<java.lang.String>,A>,Account,scala.Option<scala.math.BigDecimal>> is not compatible with cats.data.Kleisli<scala.util.Either,Account,scala.Option<scala.math.BigDecimal>>

从 Either 中删除类型将出现错误:

[error] JavaInterestService.java:13:82: scala.util.Either takes two type parameters, expected: one
[error]     public Kleisli<Either, Option<BigDecimal>, BigDecimal> computeInterest() {
[error]                    ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

sbt 配置:

val scalaTestVersion = "3.0.5"
val catsVersion = "1.6.0"

lazy val ShapelessGuide = (project in file(".")).
        settings(
            name := "test",
            version := "0.1",
            scalaVersion := "2.12.8",
            libraryDependencies ++= Seq(
                "org.scalatest" %% "scalatest" % scalaTestVersion % Test,
                "org.typelevel" %% "cats-core" % catsVersion
            ),
            scalacOptions ++= Seq(
                "-language:higherKinds",
                "-deprecation",
                "-encoding", "UTF-8",
                "-Ypartial-unification",
                "-feature",
                "-language:_"
            )
        )

我想知道是否可以在 Java 中实现 Scala 特性?或者正确的方法是什么?

尝试

public class JavaInterestService<Account> implements InterestService<Account> {
    @Override
    public Kleisli<Either, Account, Option<BigDecimal>> computeInterest() {
        throw new NotImplementedException();
    }

    @Override
    public Kleisli<Either, Option<BigDecimal>, BigDecimal> computeTax() {
        throw new NotImplementedException();
    }
}

Java 编译器可以将更高种类的 Scala 类型视为原始类型