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
}
}
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();
}
}
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 configuration:
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:_"
)
)
I'm wondering is it possible to implement a Scala trait in Java? or what's the correct way to do so?