I am trying to define a struct, and I want to add a default constructor. Additionally, I want to disable the default constructor to enforce the use of of this constructor. I did some research and found that it is possible to achieve this as follows in this link (https://forum.dlang.org/post/zmtssyredfqohwabvlnv@forum.dlang.org):
struct Test {
@disable this();
// This makes it so `Test t;` won't work - you have to use a constructor of some sort
this(int num) {
// Construct it using the number
}
// `static opCall` makes `Test()` work when you spell it out
static Test opCall() {
Test t = Test(1);
return t;
}
}
However, when I try to compile this code, it doesn't work, and I get the following error message:
Error: struct `file.Test` `static opCall` is hidden by constructors and can never be called
dlang/source/file.d(72): Please use a factory method instead, or replace all constructors with `static opCall`.
make: *** [Makefile:75: build/main] Error 1
/home/interface-d-cpp/src/interfacing-cpp/main/build
I'm wondering if there are any workarounds to make this work? It is not a significant issue as i can use factory method but it would more practical if this worked.