Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Cannot import-on-demand from object

Tags:

I am trying to make a library where you can call functions of an object through "static imports" (import-on-demand in Kotlin). Unfortunately Kotlin seems to have a limitation as seen below.

enter image description here

Why is this a limitation? Is there anything I can do to get around it? (I've tried using a class with a companion object and in that case the functions don't even resolve.)

like image 846
Jire Avatar asked Nov 15 '15 07:11

Jire


People also ask

How do I import a package to Kotlin?

To import a package in Kotlin, we use the import keyword, followed by the name of the package that needs to be imported.

How do you use the companion object in Kotlin?

To create a companion object, you need to add the companion keyword in front of the object declaration. The output of the above code is “ You are calling me :) ” This is all about the companion object in Kotlin. Hope you liked the blog and will use the concept of companion in your Android application.

Can I use Java library in Kotlin?

Yes. Kotlin provides Java language interoperability. This is a design that allows Kotlin code to transparently call Java language methods, coupled with annotations that make it easy to expose Kotlin-only functionality to Java code.

What is package level function in Kotlin?

Package-level functions are also known as top-level functions. They are declared directly inside a file without creating any class for them. They are often utility functions independent of any class: UserUtils.kt package com.app.user fun getAllUsers() { } fun getProfileFor(userId: String) { }


1 Answers

Sparkot is your Kotlin class, if I understand it's right. Objects may inherit things from supertypes, e.g. equals(), hashCode() will be imported every time you* import from an object, and the decision was made that this would be too confusing.

* - imports work only with Java object.

like image 100
Roman Belov Avatar answered Sep 22 '22 08:09

Roman Belov