scala-java-interop

access java base class's static member in scala

╄→尐↘猪︶ㄣ 提交于 2019-12-19 07:09:59
问题 Ihave some codes written in Java. And for new classes I plan to write in Scala. I have a problem regarding accessing the protected static member of the base class. Here is the sample code: Java code: class Base{ protected static int count = 20; } scala code: class Derived extends Base{ println(count); } Any suggestion on this? How could I solve this without modifying the existing base class 回答1: This isn't possible in Scala. Since Scala has no notation of static you can't access protected

Calling a protected static Java method from Scala

馋奶兔 提交于 2019-12-18 13:27:29
问题 I have a library here with some Java classes. One class has some protected static methods, which I realize is sorta an OOP no-no but I can't change its code. Assuming I have a Scala class that subclasses the aforementioned Java class, how can I call its protected static members? 回答1: See Frequently Asked Questions - Java Interoperability : This is a known limitation of Scala: there is no notion of 'static' members in Scala. Instead, Scala treats static members of class Y as members of the

How to use Java lambdas in Scala

天涯浪子 提交于 2019-12-17 13:03:33
问题 I have the following code: source .mapValues(value -> value + " Stream it!!!") .print(Printed.toSysOut()); as you can see, mapValues expects a lambda expression. Now, I am using Java library but the application is written in Scala. How to pass Scala lambda to Java code? I tried the following: source .mapValues(value => value + "hello") .print(Printed.toSysOut) But the compiler complains: [error] (x$1: org.apache.kafka.streams.kstream.Printed[String,?0(in value x$1)])Unit <and> [error] (x$1:

Visibility of properties in scala class

牧云@^-^@ 提交于 2019-12-14 00:38:42
问题 I defined a property in the constructor of my class the following way: class Step(val message:String = "") When I try access to message value from Java code y get a visbility error. Why? 回答1: If you add the @scala.reflect.BeanProperty annontation you get "automatic" get and set methods See http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html scala> class Step(@scala.reflect.BeanProperty val message:String ) defined class Step scala> val s = new Step("asdf") s: Step = Step

How to inject dependencies through Scala Reader from Java code

北城以北 提交于 2019-12-12 16:16:56
问题 Here is a dependency service: public class Service1 {} Scala code that uses it via reader: object TupleEx { type FailFast[A] = Either[List[String], A] type Env[A] = ReaderT[FailFast, Service1, A] import cats.syntax.applicative._ import cats.instances.either._ def f:Env[Int] = 10.pure[Env] } Java test where I try to inject Service1: @Test public void testf() { Service1 s = new Service1(); TupleEx.f().run(s); } I am getting an exception: Error:(10, 16) java: method run in class cats.data

how match mixed type of scala higher-kinder types feature with java generic type?

柔情痞子 提交于 2019-12-11 12:25:09
问题 my project is mixed java and scala language, but some type mismatch error occur and I think it's a common problem in term of java and scala communicate. I organized the stage with simple classes. Environment is java 1.8 and scala 2.11.7 class Item[+T](name: String) //ready use Item as MM type class Packet[+MM[_]] object GenS extends App { //use Item class def doWithPacket(packet: Packet[Item]) = {} //type error occur on packetFormJava variable form java val packetFormJava = GetGenJ

Scala Guava type mismatch issue

大城市里の小女人 提交于 2019-12-11 10:51:38
问题 I am trying to implement a simple usecase using Guava caching but facing some issues as shown below: case class Person(x:Int, y:String) val db = Map(1 -> Person(1,"A"), 2 -> Person(2,"B"), 3 -> Person(3,"C")) val loader:CacheLoader[Int,Person] = new CacheLoader[Int,Person](){ def load(key: Int): Person = { db(key) } } lazy val someData = CacheBuilder.newBuilder().expireAfterWrite(60, MINUTES).maximumSize(10).build(loader) someData.get(3) The error I am getting is related to types which I am

Using scala macro from java

那年仲夏 提交于 2019-12-11 08:09:08
问题 Currently I'm writing a scala macro library, and I would like to make it usable from java too. But I cannot think of a way to access scala macro methods form java source code. Is there any way we can use scala macro method from java. 回答1: Scala macros are instructions for scala compiler, java code is compiled by javac, which has no idea about both scala and scala macros, so I guess the answer is: there is no way to use them from java. 来源: https://stackoverflow.com/questions/25078538/using

Scala : get mixin interfaces at runtime

早过忘川 提交于 2019-12-10 16:00:05
问题 I need to get all the interfaces at runtime from a given Class (all loaded in a ClassLoader). For instance, if a class has been declared this way : trait B trait C trait D class A extends B with C with D I want to get this information at runtime : A depends on B and C and D . The java getInterfaces() (or the interfaces() from the clapper library) methods gives only the first dependency, namely: A depends on B . Is there a way to achieve that ? I guess by reflection but I don't know how ? 回答1:

How can I convert a Java map of maps for use in Scala?

风格不统一 提交于 2019-12-10 14:47:41
问题 I working on a Scala program that calls a function from a Java library, processes the results, and spits out a CSV. The Java function in question looks like this: Map<String, Map<String, AtomicLong>> getData(); The Scala: import scala.collection.JavaConversions._ def analysisAndCsvStuff(data: Map[String, Map[String, AtomicLong]]): Unit { ... } The error: type mismatch; found:java.util.Map[java...String,java...Map[java...String,java...AtomicLong]] required: scala...Map[String,scala...Map