In Scala 3, one can always write
Hamster("boo") instead of
new Hamster("boo"), even if
Hamster doesn’t name an object or a method. I love this feature, because I always thought that using
new on any public classes is a bad design choice, and in almost all cases a factory method should be used instead, as it makes a refactor much easier if some time later one decides to introduce a
MiniatureGiantSpaceHamster extends Hamster, or needs to execute code which - in Scala - cannot be executed in a constructor when creating hamsters.
Therefore, I would really like a compiler flag which forbids the use of
new A in any scope other than:
- the companion object
- a method in the same scope as the declaration of
In my eyes it improves code quality for the above reasons so much, that I can’t think of any downsides, other than backwards source compatibility and habit. However, Scala 3 already offers other optional features, such as
nullbeing only a valid value for
Null <:< AnyRef), which I also see no reason for not enabling on new projects.
If it seems like a desirable feature and someone gives me a hint where to start looking, I could try to create a pull request. I have no current knowledge of the compiler code or architecture however, so I don’t really want to spend time learning it unless there is a very high likelihood it would be integrated.