kotlin-interop

Is it possible to access a Kotlin typealias from Java?

↘锁芯ラ 提交于 2019-12-23 06:59:21
问题 Suppose I have a Kotlin 1.1 typealias for a Kotlin function type like this typealias Consumer<T> = (T) -> Unit I can access this from Java as import kotlin.Unit; import kotlin.jvm.functions.Function1; Function1<? super T, Unit> action = ... Is it somehow possible to access the Kotlin Function1 interface from Java under its Kotlin typealias name (i.e., Consumer )? 回答1: From the KEEP proposal for type aliases: NB Java has no concept of "type aliases" and can't see them in class member

Kotlin Unresolved reference: println from gradle on the CLI

[亡魂溺海] 提交于 2019-12-21 16:36:18
问题 Putting a println statement before a kotlin function returns crashes. stacktrace: thufir@dur:~/NetBeansProjects/kotlin$ thufir@dur:~/NetBeansProjects/kotlin$ gradle clean build --stacktrace w: Classpath entry points to a non-existent location: e: /home/thufir/NetBeansProjects/kotlin/src/main/kotlin/example.kt: (14, 5): Unresolved reference: println > Task :compileKotlin Using Kotlin incremental compilation FAILURE: Build failed with an exception. * What went wrong: Execution failed for task '

Kotlin: how to pass array to Java annotation

喜夏-厌秋 提交于 2019-12-21 06:56:41
问题 I want to use @OneOf annotation from package io.dropwizard.validation; Java usage: @OneOf(value = {"m", "f"}) Kotlin usage: ??? I've tried this: @OneOf(value = arrayOf("m", "f")) and this: @OneOf(value = ["m", "f"]) All i get is : Type inference failed. Expected type mismatch: required: String found: Array<String> Kotlin version: 1.1.2-2 回答1: The value parameter is automatically converted to a vararg parameter in Kotlin, as described in http://kotlinlang.org/docs/reference/annotations.html

Implement (/inherit/~extend) annotation in Kotlin

可紊 提交于 2019-12-18 03:35:25
问题 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 }

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

╄→尐↘猪︶ㄣ 提交于 2019-12-17 16:04:22
问题 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? 回答1: 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

Convert Kotlin Array to Java varargs

天涯浪子 提交于 2019-12-17 15:43:05
问题 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? 回答1: 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

Convert Kotlin Array to Java varargs

老子叫甜甜 提交于 2019-12-17 15:41:57
问题 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? 回答1: 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

Data Binding: ObservableField with lambda value doesn't compile

北城以北 提交于 2019-12-11 10:39:21
问题 I'm trying to define the visibility of the View by calculating the lambda that takes one parameter as an argument. I'm using Kotlin , btw. In my ViewModel I have: val customerPropVisibility: ObservableField<(KProperty1<Customer, *>) -> Int> = ObservableField( { _ -> // body of the lambda }) The binding expression for the View is the following: android:visibility="@{vm.customerPropVisibility.invoke(title)}" vm and title are properly declared as variables in the data tag of the layout. On

Kotlin files not able to use R.java

假装没事ソ 提交于 2019-12-10 14:19:40
问题 I created a new layout file in the appropriate resource folder. After syncing, when I try to reference the layout file, i.e. R.layout.activity_test, R is an "unresolved symbol". When I try to build in AS it fails. The interesting thing is that if I manually import the R file and use it in code, when I try to build on command line, it works. The other thing is when I try to access the layout file using R in a Java file, that also works. So I know R.java is being generated. I tried creating a

Calling kotlin functions which are keywords in java from java?

安稳与你 提交于 2019-12-10 00:42:47
问题 Since new is not a keyword in kotlin, i can have the following function in kotlin. fun new(): String { return "just returns some string" } But i am unable to call this function from java since new is a keyword in java. I would like to know if there is some alias for this function in java realm. I did not find any intellij suggestions that might be a possible alias to this function. Edit 1 : I have written the following code in kotlin: fun new(): String { return "just returns some string" }