Why can't I put the { of an anonymous class at a new line in Kotlin?

Viewed 102

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
{
};
2 Answers

Braces in the same column and indented with the line above is not Microsoft style - it's known as the Allman style. It's a style I wish people would use, but sadly K&R's ancient style was pushed by Sun (Java) as the "official" style and Kotlin has followed suit.

You can do this:

image_view.viewTreeObserver.addOnGlobalLayoutListener()
{
}

to stop Kotlin complaining.

For further information on styles, see:

https://en.wikipedia.org/wiki/Indentation_style

Related