-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Allow void function to return anything #11330
Comments
It wouldn't be a valid argument even if the language chosen for comparison wasn't developed in 10 days, not to mention dynamic and untyped. |
The goal of this feature request is very understandable but I think this would sacrifice too much safety against bugs. Return type mismatches are quite common during refactorings in my experience. It's good that tools consistently find them. |
@GSPP Your point also understandable but at least we could allow return void with void function (Debug.Log is also void function) |
You can do this today. void DoSomething(string key)
{
if(dictionary.ContainsKey(key))
Debug.Log(key + " exist"); return;
// Do some loading work
} We just don't allow My reading of your proposal is that any expressions after a |
@jmarolf Are you sure that if(dictionary.ContainsKey(key))
Debug.Log(key + " exist"); return; would not be translate to if(dictionary.ContainsKey(key))
Debug.Log(key + " exist");
return; ? I think I got Unreachable code detected warning with your code |
Ah yes you are correct! Lets pretend I wrote this if(dictionary.ContainsKey(key)){
Debug.Log(key + " exist"); return;} |
Theoretically, I wouldn't mind being able to return If we ever get |
I never understood why Surely, Has there ever been a proposal to make |
@GSPP Problem is how can you pass void in parameter and how that parameter would behave in that function. You may not allow it but then there are generic, would you allow generic to accept void If not then it has not enough purpose to allow it I just want syntactic sugar void DoSomething(string key)
{
if(dictionary.ContainsKey(key))
return Debug.Log(key + " exist");
} actually translate to void DoSomething(string key)
{
if(dictionary.ContainsKey(key))
{
Debug.Log(key + " exist");
return;
}
} Without any CLR changed At first I think it actually translate like this so it could be return any type, it will just ignore. But if you all think it too dangerous then I'm ok to just return only void function |
What's the problem with Generating a
|
Void is to emptiness. Void is sort of a placeholder for a return type, in practice. When something goes "into a void", in philosophical terms, you don't expect anything back. Now, it seems that you have a desire for something that just ends the function after calling a method.... that sounds far more reasonable as a new keyword, though the syntactic sugar value is rather low.... |
Sounds like a good use for guard blocks (#8181), although that would not reduce the verbosity but it would at least ensure that you don't accidentally forget the void DoSomething(string key)
{
guard (!dictionary.ContainsKey(key)) else
{
Debug.Log(key + " exist");
return; // would be a compiler error to omit this
}
// Do some loading work
} |
@GSPP I don't think |
@orthoxerox
This is useful when manipulating expression trees and dealing with tasks like so:
In fact it is actually defined in mscorlib: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Void.cs (attempts to use it in C# cause CS0673). This said, the value of |
I would be happy allowing |
I'm not sure I made it clear or not that what I propose is not I would let void function will return anything out of function. It just return anything to the void For example void A()
{
return 0; // not error, but it really don't return anything, just return out of stack
}
void B()
{
int i = A(); // Compile error, void function not return anything
} What I think is because some function may return a code but we don't care that code just want to call function Such as int Main(string[] cmd)
{
return 0;
}
bool already = false;
void PrepareToCallMain()
{
if(already)
return Main(["already"]); // just return to the void
// prepare everything
already = true;
} |
Doing that in JavaScript is really scary because every function returns a value. What if var debug = {
log: function (x) { return x; }
}; Then your consumers would actually get a truthy value from the method in the case where no work was performed. That would really be misleading. e.g. if(loadUserData(userName)) {
console.log('loaded data for ' + userName + ' into the cache.'); // oops
} Back in C# land, this seems like a refactoring hazard. I just don't see the value. Guard blocks are a good idea. Perhaps they could be modified to return implicitly from void DoSomething(string key)
{
guard (!dictionary.ContainsKey(key)) else
{
Debug.Log(key + " exist");
}
// Do some loading work
} |
@aluanhaddad Look at comment above you |
I read the entire discussion, what are you insinuating? |
@aluanhaddad If you really read what I said you should see the word You still state that |
@Thaina I was talking about JavaScript in reference to your original post where you use it as a point of comparison. In JavaScript every function returns a value. However, I clearly marked which language I was referring to. |
I would like at least allow return voidMethod(); as a syntactic sugar statement inside other void methods. |
Maybe use
where the void keyword indicates that we want to return and discard any return value from the called method. It would visually indicate returning nothing while still avoiding extra indentation
The point of using return void is that it both is a visual indication that we return without a value and also would allow us to use both void methods and methods returning a value in the return. The compiler will never have to guess if we wanted to return the value or ignore it, it can still give a warning if we try to return a method that returns a value and we did not use the void keyword. Void is in this case used as discards the value. |
I like this proposal just for the ability to return a void expression in a void method. I think it brings a good symmetry. |
I'm reasonably sure that at the IL layer it's happy with In a method with a void return type, I would like Of course, in a method with a non-void return type, it wouldn't be legal, since the types don't match. Somewhat related, I've occasionally found myself writing very similar code for methods that take and return |
Also I think I have seen some issue to allow |
Plus, |
@jnm2 No, you're missing the point.
If Doing this properly would require void to be the same as any other type, though, including allowing constructs like |
That's the point I was talking about. 😄 I don't think it's likely that we'll be able to deprecate |
In framework and general-use library code, sure. In application and app-specific library code, perhaps not. Though actually there might not be all that many compatibility issues with it. (If you left methods alone and simply made it apply only to async lambdas, then even less code would notice.) Still, you're right (and I said before) that it's one of those things that probably needs a time machine to solve nicely. The gang-of-four generic delegate API explosion ( |
@uecasm We already have some arguments in dotnet/csharplang#135 about that issue. And the main reason I agree that making class A<T>
{
public void Do(){}
public void Do(T obj){}
public void Do(T l,T r){}
} be ambiguous You can argue with other people there |
I don't see why that's ambiguous. Zero, one, or two parameters are still entirely distinct. I suspect we have a philosophical difference here on what So in the above, all three methods are distinct and overload resolution would happily select between them, but the JIT compiler would know it doesn't need to actually put the parameters on the stack since the method would never actually access them anyway. (If they tried, it could just use |
I found this proposal while looking for any similar open issues before proposing it for myself in https://github.com/dotnet/csharplang. I'm surprised it was not more well received. With the recent trend in C# to simplify and shorten the syntax, this one seems like a no-brainer, saving 3 lines of code (2 curly braces plus a return) for something that is very common. |
Many time I write the code that would check and return early. I want to return with one line if. But C# does not allow return void so I need to make an if block if I want to log or call some method
I want to let C# allow this
This is normal in js and I think it make code more compact
The text was updated successfully, but these errors were encountered: