如何将Scala Transformation导出为Java

问题描述 投票:1回答:1

我正在尝试用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方法返回类型已扩展为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特性?或者这样做的正确方法是什么?

java scala functional-programming scala-cats
1个回答
1
投票

尝试

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类型视为原始类型how match mixed type of scala higher-kinder types feature with java generic type?

© www.soinside.com 2019 - 2024. All rights reserved.