Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Kotlin interfaces "not open"?

In Kotlin 1.4.30, when I type

open interface I

the Kotlin compiler warns me that modifier 'open' is redundant for 'interface'. This makes complete sense: Of course interfaces are open, otherwise they would be useless.

However, the reflection library seems to contradict this:

interface I
println(I::class.isOpen) // prints 'false'

How does this make sense? The KDoc of isOpen is very brief:

true if this class is open.

What exactly is the definition of "open" in Kotlin? I thought it meant "open to the possibility of being sub-typed by classes outside this file".

like image 694
blubb Avatar asked Feb 26 '21 13:02

blubb


1 Answers

The methods isFinal, isOpen, isAbstract, isSealed were designed such that only one of them returns true for all KClass instances.

Source: this comment in KT-19850.

Since interfaces are abstract, I::class.isAbstract == true. Combined with the above design goal, I::class.isOpen == false results.

Please upvote KT-19850 to help getting this surprising behavior fixed.

like image 52
blubb Avatar answered Oct 24 '22 07:10

blubb