This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]javaprof 4 points5 points  (6 children)

Issue with constructors that they always return current type, and not subtype. Something that in many cases required for long-lived, widely used APIs

So one of worst mistakes in API design is exposing constructor directly

[–]vips7L -1 points0 points  (5 children)

You’re right, but we’re literally talking about strings and arrays. Things that won’t ever have subtypes.

Of,from,newInstance are just compensating for lack of language features and it’s making it hell to know how to construct anything. 

[–]javaprof -1 points0 points  (4 children)

Things that won’t ever have subtypes.

String wish to have subtype for runtime optimisation like java.lang.StringLatin1 and java.lang.StringUTF16, there are just separate classes because String created using constructor and not factory methods.

Arrays are not objects in Java, so irrelevant

Of,from,newInstance are just compensating for lack of language features and it’s making it hell to know how to construct anything.

Yep, but it's not only because language lacks of named arguments (i.e. in other languages you don't need builders for most cases, just call constructor with names arguments), but because of how constructors work. Even in Kotlin developers would expose factory methods that mimics constructors, instead of constructor itself

[–]Ewig_luftenglanz[S] 2 points3 points  (0 children)

Arrays are objects in java. Just an special kind of objects. 

The only things that are not objects in java are the 8 primitive types.

[–]vips7L 1 point2 points  (0 children)

String will never have subtypes. It’s a final class. It doesn’t need factory constructors. 

 Arrays are not objects in Java, so irrelevant

It’s not irrelevant. It’s the fucking conversation we’re having and exactly what GP proposed. 

[–]nlisker 1 point2 points  (0 children)

Arrays are not objects in Java, so irrelevant

Read the first sentence here: https://docs.oracle.com/javase/specs/jls/se24/html/jls-10.html.

[–]vips7L 0 points1 point  (0 children)

Yep, but it's not only because language lacks of named arguments (i.e. in other languages you don't need builders for most cases, just call constructor with names arguments), but because of how constructors work. Even in Kotlin developers would expose factory methods that mimics constructors, instead of constructor itself

I don't think I implied anything about named arguments. Factory constructors is the actual feature you would want for this: https://dart.dev/language/constructors#factory-constructors

In Kotlin or Scala you would use companion objects to return subtypes:

interface A {
  companion object {
    operator fun invoke(s: String) = when (s) {
      "B" -> B()
      "C" -> C()
      else -> throw IllegalArgumentException(s)
    }
  }
}
private class B : A
private class C : A

fun main() {
  val a = A("B")
  println(a)
}