-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Support for automatic output of return type in function signatures #3682
Comments
I don't like this. I want to see a function signature and know what it returns. |
No. The function body will define return type as it is. It will be easier for you to write code, it will not affect users of your lib, etc. It will be the same |
The Rust norm is to be explicit in the function signatures to ensure that the interface to the function is correct, and then validate the function implementation against that. See, for example, this FAQ answer.. |
One doesn't necessarily have IDE, especially when reading foreign code. |
to reduce repetition #3444 i.e. putting the inference on the value rather than the type is getting more traction. // instead of
fn rotate(angle: f64) -> _ {
let (s, c) = angle.sin_cos();
Mat2 { entries: [[c, -s], [s, c]] }
}
// you write
fn rotate(angle: f64) -> Mat2<f64> {
let (s, c) = angle.sin_cos();
_ { entries: [[c, -s], [s, c]] }
} |
It's just syntax sugar, it does not affect the language itself The problem with foreign code reading via eyes, not tools is open for discussion |
Extending the type system to allow function signatures to infer their return types is not syntax sugar. It affects the language and the type system. It doesn't matter if the function returns a single value. For example,
Now introduces a dependency edge between type-checking |
F# supports both
The following answers suggest that it is not so simple.
|
Short Description:
Add the ability to use the underscore character (_) (or similar) to indicate the return type in function signatures in Rust.
This would allow the compiler to automatically determine the type of the return value based on the function body, simplifying syntax in cases where the type is obvious from context.
Motivation:
Currently in Rust, when writing functions, you must explicitly specify the return type in the signature. In many cases, the type of the return value is obvious from context, and having to explicitly specify it adds unnecessary verbosity. Supporting automatic output of the return type will:
Examples:
С++ References
Unresolved issues:
Should this function be limited to certain types of functions (for example, functions with a single return statement)?
How will this function interact with the existing type inference mechanisms in Rust, especially in more complex cases involving universal functions or multiple returned data?
Future opportunities:
Extending the idea of type inference to other parts of the function signature, such as parameter types, where appropriate.
Conclusion:
The ability to automatically determine the type of the return value in function signatures will provide more convenient and readable encoding, especially in simple cases when the type of the return value is obvious from the context. However, care should be taken to ensure that this function does not introduce unnecessary ambiguity and does not complicate debugging.
The text was updated successfully, but these errors were encountered: