Questions tagged [kotlin-interop]

Usage of Kotlin together with other languages, most notably Java. e.g. Calling Kotlin functions from Java, or using Java classes from Kotlin.

117 questions
278
votes
1 answer

Convert Kotlin Array to Java varargs

How can I convert my Kotlin Array to a varargs Java String[]? val angularRoutings = arrayOf("/language", "/home") // this doesn't work web.ignoring().antMatchers(angularRoutings) How to pass an ArrayList to a varargs method…
robie2011
  • 3,678
  • 4
  • 21
  • 20
71
votes
3 answers

Private getter and public setter for a Kotlin property

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…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
71
votes
2 answers

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

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());,…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
55
votes
14 answers

Comparing two lists in kotlin

I came across with kotlin equals function to compare two list of same type. It works fine for pure Kotlin with data classes. I'am using a Java library in Kotlin project in which a callback method returns a list of objects for a time interval of X…
Aswin
  • 1,154
  • 1
  • 11
  • 29
43
votes
7 answers

how to use spring annotations like @Autowired or @Value in kotlin for primitive types?

Autowiring a non-primitive with spring annotations like bellow works: @Autowired lateinit var metaDataService: MetaDataService But this doesn't work: @Value("\${cacheTimeSeconds}") lateinit var cacheTimeSeconds: Int Error: lateinit modifier is…
fkurth
  • 868
  • 1
  • 8
  • 11
42
votes
5 answers

Kotlin extension function access Java private field

I'd like to access Java's private field when using Kotlin extension function. Suppose I have a Java class ABC. ABC has only one private field mPrivateField. I'd like to write an extension function in Kotlin which uses that field for whatever…
37
votes
4 answers

Kotlin: how to pass array to Java annotation

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"]) (EDIT: this example works…
charlie_pl
  • 2,898
  • 4
  • 25
  • 39
36
votes
5 answers

How to implement this Java interface in Kotlin?

Since Kotlin doesn't have primitives, how could this interface be implemented? public interface A { @NotNull Object get(@NotNull Integer i); @NotNull Object get(int i); } I cannot change the Java code since it's a compiled class file in a…
ice1000
  • 6,406
  • 4
  • 39
  • 85
26
votes
1 answer

Is it possible to access a Kotlin typealias from Java?

Suppose I have a Kotlin 1.1 typealias for a Kotlin function type like this typealias Consumer = (T) -> Unit I can access this from Java as import kotlin.Unit; import kotlin.jvm.functions.Function1; Function1 action = ... Is it…
Anlon Burke
  • 397
  • 4
  • 12
26
votes
6 answers

Assignment not allowed in while expression?

In Java we can usually perform an assignment within the while condition. However Kotlin complains about it. So the following code does not compile: val br = BufferedReader(InputStreamReader( conn.inputStream)) var output:…
PedroD
  • 5,670
  • 12
  • 46
  • 84
22
votes
3 answers

Calling kotlin functions which are keywords in java from java?

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…
solo_assassin
  • 483
  • 4
  • 20
21
votes
5 answers

Kotlin Native how to convert ByteArray to String?

I was playing with the kotlin-native samples. I wonder how I could get String from pinned ByteArray. Just want to print it in the console.
Stepango
  • 4,721
  • 3
  • 31
  • 44
20
votes
2 answers

Call Kotlin object with class delegation from Java as a static method

This may be a bit difficult to describe, so I'll try to give a concrete example of what I'm trying to do. Suppose we have a Facade interface and class (in Java), like this: interface FacadeInterface { void method(String from, String…
jvincek
  • 560
  • 4
  • 14
19
votes
2 answers

Is it possible to build a full Node.js web application using Kotlin?

If I understand correctly, with the release of Kotlin 1.1, we can set JavaScript as a compile target for full compilation to JavaScript of Kotlin projects. Is it possible (or feasible) to write an entire Node.js application, such as an express…
Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
17
votes
1 answer

Build errors with new kotlin 1.1 , kapt cant parse databinding params

I updated my project from android studio 2.2 to android studio 2.3 and incremented the project from kotlin 1.06 to kotlin 1.1 This is the error I get …
1
2 3 4 5 6 7 8