logoalt Hacker News

vips7Lyesterday at 2:56 PM1 replyview on HN

I really wish the JDK devs would start using constructors again. I understand why factory methods exist but everything these days is of/from/newInstance/something else. Dart/Scala/Kotlin have all really solved this with language features and construction is uniform. For example in Dart they have factory constructors: https://dart.dev/language/constructors#factory-constructors


Replies

vips7Lyesterday at 5:46 PM

and for Kotlin/Scala you can use some cheeky operator overloading via companion objects.

    sealed Interface A {
        class B: A
        class C: A
        companion object {
            operator fun invoke(s: String) = when (s) {
                "B" -> B()
                "C" -> C()
            }
        }
    }

    val a = A("B")