jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Presence of create() function produces error of function re-declaration

Open mnlrsn opened this issue 2 years ago • 1 comments

To reproduce:

class C {
    public function create() throws {
        return C()
    }
}

function main() {
    println("{}", C::create())
}

Error message:

error: class member cannot be redeclared
static ErrorOr<NonnullRefPtr<C>> create() { auto o = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) C ())); return o; }
                                 ^
build/no-return-type.cpp:7:34: note: previous declaration is here
static ErrorOr<NonnullRefPtr<C>> create();
                                 ^

mnlrsn avatar Aug 18 '22 22:08 mnlrsn

For clarity, there is a workaround:

Re-name the function, for instance to creat().

mnlrsn avatar Aug 24 '22 07:08 mnlrsn

Yes, because create is used as codegened name for the function, at least for classes. It was likely done like this to mimic what is done in serenity in many places where a public create function creates a shared pointer (jakt classes are always shared too) and the constructor is made private. I cannot see any real functional reason. I would suggest reserving double underscore prefix for codegen and codening as __create. @jntrnr @awesomekling ?

maddanio avatar Oct 03 '22 16:10 maddanio