Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to yourself in the anonymous class?

Tags:

kotlin

I have next code in kotlin:

handler.postDelayed(object : Runnable {
            override fun run() {
                Timber.i("run post msg")
                handler.postDelayed(this, AppPrefs.SEARCH_DELAY)
            }
        },AppPrefs.SOCKET_INTERVAL)

how you see it's simple standard way to create delayed task (with Runnable class). Value this references to anonimus Object implements Runnable and compile and works fine

But when i make lamdba for this:

handler.postDelayed({
            Timber.i("run post msg")
            handler.postDelayed(this, AppPrefs.SOCKET_INTERVAL)
        },AppPrefs.SOCKET_INTERVAL)

value this referenced to outher class.

How referenced from inner anonimus class to yourself?

like image 442
abbath0767 Avatar asked Jun 22 '17 10:06

abbath0767


People also ask

How do you call an anonymous class in Java?

Object = new Example() { public void display() { System. out. println("Anonymous class overrides the method display()."); } }; Here, an object of the anonymous class is created dynamically when we need to override the display() method.

How do you make an anonymous class?

The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code. A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time.

What's meant by Anonymous class explain with example?

Java anonymous inner class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as overloading methods of a class or interface, without having to actually subclass a class.

What is the use of anonymous class?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

What is anonymous class in Java?

A nested class that doesn't have any name is known as an anonymous class. An anonymous class must be defined inside another class. Hence, it is also known as an anonymous inner class. Its syntax is: Anonymous classes usually extend subclasses or implement interfaces.

What is an anonymous class in Kotlin?

The anonymous class is one of the features and concepts for the kotlin language. It is used to create the class instance by using the object expression. The object declarations are one of the singleton object patterns where the class can have only one instance; it is more useful for to working the backend like databases etc.

What does the semicolon mean at the end of anonymous classes?

Note: Anonymous classes are defined inside an expression. So, the semicolon is used at the end of anonymous classes to indicate the end of the expression. Inside an anonymous class.

What do you say when someone asks why you are taking classes?

Share why you are taking the class. If you’re in a college course, you can share your intended major. Or if you’re taking a class for a certification for a job, you could share what it is you do for work. For example, you could say, “Hi, I’m Mark, Mark Palmer.


1 Answers

You cannot do this. A similar question was asked on Kotlin's forum and yole (one of the creators of the language) said this:

this in a lambda refers to the instance of the containing class, if any. A lambda is conceptually a function, not a class, so there is no such thing as a lambda instance to which this could refer.

The fact that a lambda can be converted into an instance of a SAM interface does not change this. Having this in a lambda mean different things depending on whether the lambda gets SAM-converted would be extremely confusing.

like image 102
Mibac Avatar answered Oct 06 '22 23:10

Mibac