gpt4 book ai didi

java - 如何将 Scala 转换导出到 Java

转载 作者:行者123 更新时间:2023-11-30 10:04:59 24 4
gpt4 key购买 nike

我正在尝试通过 Java 实现 Scala trait,该 trait 在另一个容器中有一个通用容器类型,无法通过 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 特性?或者这样做的正确方法是什么?

最佳答案

尝试

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?

关于java - 如何将 Scala 转换导出到 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55528032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com