-
Notifications
You must be signed in to change notification settings - Fork 258
[BUG] short function body syntax generates bad code for named functions #257
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
Comments
Thanks! You may have come across an example where my current choice of different defaults for named and unnamed functions may be a problem. Here's why this is currently by-design:
But now when you additionally throw a single-expression body into the mix, you get the effect you see above. What do you think... is this too surprising, so that (probably the better resolution) unnamed functions would also default to no return, and you'd have to write |
Brainstorming:
Okay, it just hit me (after writing the below) that what I wrote here is exactly how
Is this only when the type of the return is based on the type of one or more arguments? Is this a fundamental limitation, or just a limitation of the conversion to C++1?
I think defaulting to I'm thinking this might fall into don't pay for what you don't use. If you don't need order independence, then you can use defaulted return types. If you want order independence for your functions, then you have to pay for it by having explicit return types for them. The question then becomes, can we easily determine at the point of a function call that if a function had an explicit return type, then we could call it, and we can tell the user this? |
I am aware that What I started to like about cppfront is that it is consistent regarding the syntax. The more exceptions to the rules, the more we need to learn how it works. That is worth fighting for. Maybe this is one of the alpha limitations. |
@filipsajdak I hear you... I'll likely switch this to making @gregmarr I hear you too, arguing in favor of the consistent default being Thanks for the input! |
An interesting observation is that I probably would never have even suggested this if I hadn't started using more C# and Typescript recently and gotten used to Also, in the end, I wasn't as much arguing for |
Yes, my experience with lambdas in other languages is what made me default to FWIW, here in cppfront, the one small usage-based data point I have so far about making
I'll keep an eye on it... |
I don't think I ever consciously noticed that, so that's a good thing. I do have under 2 years regular experience with C# now, so I'm still learning.
I would be interested to see what those same changes would be with
Was this found because you had an expression function that returned something, and then assigned that return value to a variable? I assume then that any other expression functions that you had that returned something had an explicit return type, including
I could see this being something that |
It was this:
|
Oh, interesting, mismatched function pointer types, that's not what I would have expected. |
This is an omnibus commit of the last few evenings' changes. Primarily it was to start laying the groundwork for constructors, but it includes other fixes and closes a few issues. Details: - Begin infrastructure preparation for constructors - Started creating navigation APIs to replace the low-level graph node chasing; this makes cppfront's own code cleaner and the tree easier to change if needed, but it's also a step toward a reflection API - Extended `main:(args)` to require the name "args" for the single-parameter `main`, and to support `args.argc` and `args.argv`, further on hsutter#262 (see comment thread) - Changed default return type for unnamed functions to be `-> void`, same as named functions, closes hsutter#257 - Disallow single-expression function body to be just `return`, closes hsutter#267 - Make `make_args` inline, closes hsutter#268 - Bug fix: emit prolog also for single-expression function body. Specifically, this enables main:(args)=expression; to work correctly. Generally, this also corrects the code gen for examples that were trying (but failing) to inject prologs in single-expression functions... in the regression tests this corrected the code gen for `pure2-forward-return.cpp2` which was missing the contract check before.
The current implementation of the cppfront compiles the following code:
to
Expectation
I expect that the named and unnamed function defined with short function body syntax generates the same code that uses implicit return.
The text was updated successfully, but these errors were encountered: