Would like to have 'open sealed class' such that l...
# language-proposals
p
Would like to have 'open sealed class' such that like normal sealed class but also can be instantiated. Use case (it is not real live example but shows the problem) :
Copy code
open sealed class HistoryItem( // make it open to omit adding additional class. Something like: class StdHistoryItem(...) : HistoryItem(...)
  val ts: Long,
  val info: String
) {

  open fun log() {
    println("[$ts] $info")
  } 
}

class WithContextHistoryItem(
  ts: Long,
  info: String,
  context: Any
) : HistoryItem(ts, info) {

  override fun log() {
    println("[$ts] $info ($context)")
  }
}

fun main() {
  val items = listOf( HistoryItem(1, "a"),  WithContextHistoryItem(2, "b", "c") )
  ... 
}
k
This would add more cognitive complexity as it would require you to remember to add the base class last in exhaustive when expressions.
p
Good point.
k
Also, to quote from Scott Meyer's "Effective C++" a phrase which is relevant to all OOP languages: "replacing concrete base classes with abstract base classes forces you to explicitly recognize the existence of useful abstractions".
s
why is adding a "standard" class (as in your example) undesirable? Just to save on typing?
p
Thought that it is redundant. But now change my mind.
👍 1