Skip to content

Discussion: class member functions should be const by default #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marzojr opened this issue Sep 30, 2022 · 1 comment
Closed

Discussion: class member functions should be const by default #30

marzojr opened this issue Sep 30, 2022 · 1 comment

Comments

@marzojr
Copy link

marzojr commented Sep 30, 2022

I know that class are still not supported on cppfront; but this makes the discussion even more relevant.

One of the things that lambda functions (arguably) get right is making their operator() const by default and requiring mutable if you want to change data from the lambda.

This should be done also for cpp2 classes. This has the advantages of (1) making lambdas less of a special case, and (2) making const-correct code the default.

This is kind of related to issue #25 .

@hsutter
Copy link
Owner

hsutter commented Oct 2, 2022

That's the intent, and it just falls out of making in parameters the default. Classes will use explicit this which, like all parameters, defaults to in and therefore const.

I've added this to the Design note: const objects by default. Thanks!

@hsutter hsutter closed this as completed Oct 2, 2022
JohelEGP referenced this issue Nov 26, 2023
This example now works:

    main: () = {
        v: std::vector = ( 1, 2, 3, 4, 5 );

        //  Definite last use of v => move-capture v into f's closure
        f := :() -> forward _ = v$;

        //  Now we can access the vector captured inside f()...
        f().push_back(6);
        for f() do(e) std::cout << e;       // prints 123456
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants