Date: Wed, 6 Apr 2022 14:59:59 +0300
Hi,
I'm not sure if something like this has already been proposed but I was
thinking about the following idea:
I see a lot of function definitions of the following form:
```
auto func_name(<args>) -> ReturnType
{
ReturnType ret_val {<initial_value>};
//do something with ret_val
return ret_val;
}
```
What if we could make the compiler generate the initialization and the
return statement automatically?
Consider the following function signature and body:
```
auto func_name(<args>) -> ReturnType ret_val { <initial_value> }
{
//do something with ret_val
}
```
The compiler would transform this into:
```
auto func_name(<args>) -> ReturnType
{
ReturnType ret_val { <initial_value> }; //implicitly generated by the
compiler
{
//do something with ret_val
}
return ret_val; //implicitly generated by the compiler
}
```
The compiler would then (possibly) do NRVO on ret_val.
The rest of the language rules apply the same way.
You can do early returns or you can return different objects:
```
auto func_name(<args>) -> ReturnType ret_val { <initial_value> }
{
//do something with ret_val
if(early_return)
return ret_val;
//do something more
if(return_different_object)
return some_other_value;
}
```
This feature also allows for "shorthand function definitions":
```
auto func_name(<args>) -> ReturnType ret_val { <init_value> } {};
```
The whole body of the function is in the initialization.
Although, to be fair, in this case you can simply write it the normal way:
```
auto func_name(<args>) -> ReturnType { return { <init_value>}; }
or
ReturnType func_name(<args>) { return { <init_value> }; }
```
Some clarifications:
1) The initialization of the named return value is required because if it's
optional the compiler is not able to tell if the curly braces belong to the
initialization or the function body.
So, you either supply both the return value name and the initialization or
none.
2) The initialization could be empty, i.e. -> ReturnType ret_val {};
3) If you cannot not supply an initial value to the return type then you
cannot give a name to the return value and syntax we currently have.
4) I think it should be legal to write the function declaration without the
return value name and initial value and then supply them in the function
definition.
This is already the case for the names of the function arguments.
5) I was thinking about an alternative syntax for this feature.
For example, instead of an actual name to the return value use the 'return'
keyword.
The benefit is that you can easily search all the places where the function
returns.
The drawback is you lose the identifier of the return value.
Another possibility is to have both the 'return' keyword and the name:
```
auto func_name(<args>) -> return ReturnType ret_val { <init_value> };
```
Although, to be frank, this syntax looks a bit weird.
6) I'm not sure how this feature would integrate with coroutines.
What do you think about this idea?
Best regards,
Zamfir
I'm not sure if something like this has already been proposed but I was
thinking about the following idea:
I see a lot of function definitions of the following form:
```
auto func_name(<args>) -> ReturnType
{
ReturnType ret_val {<initial_value>};
//do something with ret_val
return ret_val;
}
```
What if we could make the compiler generate the initialization and the
return statement automatically?
Consider the following function signature and body:
```
auto func_name(<args>) -> ReturnType ret_val { <initial_value> }
{
//do something with ret_val
}
```
The compiler would transform this into:
```
auto func_name(<args>) -> ReturnType
{
ReturnType ret_val { <initial_value> }; //implicitly generated by the
compiler
{
//do something with ret_val
}
return ret_val; //implicitly generated by the compiler
}
```
The compiler would then (possibly) do NRVO on ret_val.
The rest of the language rules apply the same way.
You can do early returns or you can return different objects:
```
auto func_name(<args>) -> ReturnType ret_val { <initial_value> }
{
//do something with ret_val
if(early_return)
return ret_val;
//do something more
if(return_different_object)
return some_other_value;
}
```
This feature also allows for "shorthand function definitions":
```
auto func_name(<args>) -> ReturnType ret_val { <init_value> } {};
```
The whole body of the function is in the initialization.
Although, to be fair, in this case you can simply write it the normal way:
```
auto func_name(<args>) -> ReturnType { return { <init_value>}; }
or
ReturnType func_name(<args>) { return { <init_value> }; }
```
Some clarifications:
1) The initialization of the named return value is required because if it's
optional the compiler is not able to tell if the curly braces belong to the
initialization or the function body.
So, you either supply both the return value name and the initialization or
none.
2) The initialization could be empty, i.e. -> ReturnType ret_val {};
3) If you cannot not supply an initial value to the return type then you
cannot give a name to the return value and syntax we currently have.
4) I think it should be legal to write the function declaration without the
return value name and initial value and then supply them in the function
definition.
This is already the case for the names of the function arguments.
5) I was thinking about an alternative syntax for this feature.
For example, instead of an actual name to the return value use the 'return'
keyword.
The benefit is that you can easily search all the places where the function
returns.
The drawback is you lose the identifier of the return value.
Another possibility is to have both the 'return' keyword and the name:
```
auto func_name(<args>) -> return ReturnType ret_val { <init_value> };
```
Although, to be frank, this syntax looks a bit weird.
6) I'm not sure how this feature would integrate with coroutines.
What do you think about this idea?
Best regards,
Zamfir
Received on 2022-04-06 12:00:12