Skip to content
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

[SUGGESTION] Removing special handling of return value of main function #254

Closed
equeim opened this issue Feb 9, 2023 · 5 comments
Closed

Comments

@equeim
Copy link

equeim commented Feb 9, 2023

Currently Cpp2 allows this code:

main: () -> int = {
    std::cout << "Hello \n";
}

I think this code is problematic because it says main function returns an int but it doesn't actually do this. It's especially bad for beginners since one of the first things you would have to explain is why it's allowed here and not in any other place, instead of more substantial things.

Instead I propose allowing to declare main function without return value (and disallowing previous example if it doesn't have return statement):

main: () = {
    std::cout << "Hello \n";
}

It will generate "auto main() -> int" that will return 0.
This way people who start to learn Cpp2 won't immediately ask "why it doesn't return int" (maybe). And the topic of special properties of main function and what forms it can take can be taught later.

@MichaelCook
Copy link

Only loosely related to this issue, but cpp2 could also easily allow other variants for main's signature, such as:

main: (std::span<char*> args) = {

@SebastianTroy
Copy link

SebastianTroy commented Feb 10, 2023 via email

@filipsajdak
Copy link
Contributor

@MichaelCook & @SebastianTroy it was already discussed here: #63 (comment) and here #45 (comment)

@equeim
Copy link
Author

equeim commented Feb 10, 2023

I think this suggestion would be easier to implement than changing main's arguments. You already can't declare void main() in C++ so it won't conflict with anything. It's just a simple transformation of return type and possibly an addition of return 0; (though it can be omitted since C++ allows it).

@hsutter
Copy link
Owner

hsutter commented Mar 1, 2023

Limiting the discussion to the return type: Yes, currently all named functions default to returning -> void, and I've had an itch in the back of my mind to allow that to work for main too. Done... commit coming...

(I do it by just emitting Cpp2 -> int anyway under the covers. But in Cpp2 it's still truly a -> void default, because if you use that default you can't write return statements in the Cpp2 main body. Even though we emit -> int to Cpp1 to keep today's compilers happy.)

Thanks!

@hsutter hsutter closed this as completed in a6e383b Mar 1, 2023
zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
For example, this is now a complete legal program in pure Cpp2 (with `-p`) with fully conformant Cpp1 code gen: `main: () = std::cout << "Hello world";`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants