kotlin-interop

Implement (/inherit/~extend) annotation in Kotlin

心不动则不痛 提交于 2019-11-29 02:04:39
In Java I have the possibility to "implement" annotations. Sample Java annotation: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface JavaClassAnno { String[] value(); } Sample Java "implementation": class MyAnnotationLiteral extends AnnotationLiteral<JavaClassAnno> implements JavaClassAnno { // <--- works in Java private String value; public MyAnnotationLiteral(String value) { this.value = value; } @Override public String[] value() { return new String[] { value }; } } Trying to port that to Kotlin doesn't work as it says that the annotation is final and therefore

Why do I have to return Unit.INSTANCE when implementing in Java a Kotlin function that returns a Unit?

早过忘川 提交于 2019-11-28 22:30:11
If I have a Kotlin function fun f(cb: (Int) -> Unit) and I want to call f from Java, I have to do it like: f(i -> { dosomething(); return Unit.INSTANCE; }); which looks very ugly. Why can't I just write it like f(i -> dosomething()); , since Unit in Kotlin is equivalent to void in Java? Unit in Kotlin is mostly equivalent to void in Java, however only when the rules of the JVM allow it. Functional types in Kotlin are represented by interfaces like: public interface Function1<in P1, out R> : Function<R> { /** Invokes the function with the specified argument. */ public operator fun invoke(p1: P1

Private getter and public setter for a Kotlin property

妖精的绣舞 提交于 2019-11-28 08:53:59
How to make a property in Kotlin that has a private getter (or just do not have it) but has a public setter? var status private get doesn't work with an error: Getter visibility must be the same as property visibility In my case, the reason is for Java interop: I want my Java code to be able to call setStatus but not getStatus . It's impossible at the moment in Kotlin to have a property with a setter that is more visible than the property. There's a language design issue in the issue tracker on this, feel free to watch/vote for it or share your use cases: https://youtrack.jetbrains.com/issue

Convert Kotlin Array to Java varargs

谁说胖子不能爱 提交于 2019-11-27 23:24:26
How can I convert my Kotlin Array to a varargs Java String[] ? val angularRoutings = arrayOf<String>("/language", "/home") // this doesn't work web.ignoring().antMatchers(angularRoutings) How to pass an ArrayList to a varargs method parameter? You should use the " spread operator ", which looks like this: * The spread operator needs to be prefixed to the array argument: antMatchers(*angularRoutings) For further information, see the documentation : When we call a vararg -function, we can pass arguments one-by-one, e.g. asList(1, 2, 3) , or, if we already have an array and want to pass its

Passing a listener object as a function parameter in kotlin

*爱你&永不变心* 提交于 2019-11-27 07:55:09
问题 I'm trying to pass a listener from an action to a class (an adapter). In java (code from the Action): private void setListeners() { adapterRecyclerView.setListener( new View.OnClickListener() { @Override public void onClick(View v) { SomeCodehere.... } }); } (code from the adapter) public void setListener(View.OnClickListener listener) { this.listener = listener; } It works. Now I'm trying to traslate to kotlin. I translate first the action (translation the action to kotlin): private fun

Private getter and public setter for a Kotlin property

落爺英雄遲暮 提交于 2019-11-27 02:07:23
问题 How to make a property in Kotlin that has a private getter (or just do not have it) but has a public setter? var status private get doesn't work with an error: Getter visibility must be the same as property visibility In my case, the reason is for Java interop: I want my Java code to be able to call setStatus but not getStatus . 回答1: It's impossible at the moment in Kotlin to have a property with a setter that is more visible than the property. There's a language design issue in the issue