如何将Scala转换导出到Java [英] How to export Scala Transformation to Java

查看:479
本文介绍了如何将Scala转换导出到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement a Scala trait by Java, the trait has a generic container type within another container, which couldn't be solved automatically by java import, the Scala code is below:

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会自动生成方法:

Once it has been implemented by Java, Intellij automatically generates methods:

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]携带.错误消息是:

Now You will see the java method return type has been extended to Either<NonEmptyList<String>, A> which still has an generic parameter A inside, which is carried form Scala type Valid[A]. Error message is:

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>>

从任一位置删除类型都会收到错误消息:

Remove type from Either will get error:

[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特性?还是这样做的正确方法是什么?

I'm wondering is it possible to implement a Scala trait in Java? or what's the correct way to do so?

推荐答案

尝试

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类型视为原始类型 如何匹配混合类型Java泛型的scala更高类​​型类型功能的实现?

Java compiler can see higher-kinded Scala types as raw types how match mixed type of scala higher-kinder types feature with java generic type?

这篇关于如何将Scala转换导出到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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