Hacker News new | ask | show | jobs
by vips7L 4 days ago
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
1 comments

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")