This question may be stupid but... why? Personally I like the Microsoft style where { is at the same column as the matching }. In all languages I have used, it did not matter where { was placed.
But in Kotlin, only this works.
image_view.viewTreeObserver.addOnGlobalLayoutListener{
};
And this causes an error.
image_view.viewTreeObserver.addOnGlobalLayoutListener
{
};
In kotlin, we can achieve this by using the “Object” keyword; the object declaration contains properties, methods, and so on. However, mainly it does not allowed the constructor to create the object. Like that object, a keyword is used for to create the objects of the anonymous class known as the anonymous object.
Since anonymous inner class has no name, an anonymous inner class cannot have an explicit constructor in Java.
Object a = new Class1(){ void someNewMethod(){ } public XXXXXXXX(int a){ super(); System. out. println(a); } };
https://kotlinlang.org/docs/reference/grammar.html#semicolons
Because your second example has the same meaning as
image_view.viewTreeObserver.addOnGlobalLayoutListener;
{
};
a property access followed by an empty lambda.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With