How to disable/hide constructor when using factory method?

Alex sascha.orlov at gmail.com
Wed Jan 23 19:41:44 UTC 2019


On Wednesday, 23 January 2019 at 19:26:37 UTC, JN wrote:
> class Foo
> {
>     static Foo makeFoo()
>     {
>         Foo f = new Foo();
>         return f;
>     }
> }
>
> void main() {
>     Foo f = Foo.makeFoo();
> }
>
> For a code like this. I'd like all users of the class to be 
> forced to create instances using the static method makeFoo. I 
> want to disallow "new Foo()". But I don't know if it's possible 
> to disable the constructor, while still making it available in 
> the makeFoo static method.
>
> @disable this doesn't work, private this(); doesn't work either.

private should work, if the class and the main are in different 
modules, no?


More information about the Digitalmars-d-learn mailing list