Replies: 78 comments 5 replies
-
I think it looks confusing, because it looks as if I am really returning something. void LoadAndDoSomething(string url)
{
var obj = Load(url);
if(obj == null)
{ Console.WriteLine("Not found " + url); return; }
// Do Something
} though putting the return on a single line would be better, because you should always mark the places good visible when returning before the end of a function. |
Beta Was this translation helpful? Give feedback.
-
This hack is so dirty. Too dirty. And will be gone with auto formatting While in any other return type function except void we could return function that has same return type int CountOrSingle(IEnumerable<int> ints)
{
int i = Load(url);
if(ints.Count() > 1)
return ints.Count();
return ints.Single();
} To not allow return void actually make inconsistent syntax |
Beta Was this translation helpful? Give feedback.
-
@Thaina
That's why we should write it in two lines. |
Beta Was this translation helpful? Give feedback.
-
Yes, I return void |
Beta Was this translation helpful? Give feedback.
-
I'd like to be able to use edit: Right now there is an annoying quirk that you need to double your api size to support the dichotomy of generic things that return values and ones that don't. |
Beta Was this translation helpful? Give feedback.
-
In my understanding of the word 'void' it is not kind of a type |
Beta Was this translation helpful? Give feedback.
-
Well it is a type in .Net "System.Void" but it is not exposed to high level languages! |
Beta Was this translation helpful? Give feedback.
-
Instead of a new language feature, just use a type that can have exactly I have an implementation here But at its simplest: public struct Unit : IEquatable<Unit>
{
public static readonly Unit Default = new Unit();
public override int GetHashCode() => 0;
public override bool Equals(object obj) => obj is Unit;
public override string ToString() =>"()";
public bool Equals(Unit other) => true;
public static bool operator ==(Unit lhs, Unit rhs) => true;
public static bool operator !=(Unit lhs, Unit rhs) => false;
} Also if you define a static field like so: public static class Prelude
{
public readonly static Unit unit => Unit.Default;
} And include the containing class: using static Prelude; Then you can do stuff like this: public Unit Foo()
{
// ... Do something ...
return unit;
} Which makes the method usable in plenty of places where a return type is needed (LINQ expressions, |
Beta Was this translation helpful? Give feedback.
-
@lachbaer Well , in my understanding, void is nothing. And that's it, nothing is a thing and we can return nothing. It abstraction like we return void of space is common wording But anyway. I don't think we need that much philosophy. I just think it useful to keep code compact and being one line Also in lambda expression. It can assume that you return void when you call void function in lambda. By this assumption I think anywhere should be the same I mean int X() { return 0; }
void A() { }
Func<int> action = () =>X(); // lambda can guess that you return int from int function, so this is int function
Action action = () =>A(); // lambda can guess that you return void from void function, so this is void function |
Beta Was this translation helpful? Give feedback.
-
'no thing' is not a thing. Your understanding is wrong I'm afraid. |
Beta Was this translation helpful? Give feedback.
-
@louthy What you said is like 0 is not a thing. Space is not a thing You are not wrong. We have zero things means we don't even have a thing. But I think it's all about context. We have zero as number. We has spacebar and space character to make space. And we have void of space to define volume that really have nothing But is empty volume is a thing? As someone already pointed out. We even have Programming make use of abstraction and we make abstraction of void mean a thing in programming. So we can have void as return type. Why we can have return type be void but cannot return void? I think we actually always return void |
Beta Was this translation helpful? Give feedback.
-
Take a look at Core Computational Type Theory
Personally I would love to see all uses of |
Beta Was this translation helpful? Give feedback.
-
@louthy I don't think you need to change any meaning of Like you said, in context of programming 0 is number. But in context of natural number. 0 is not a number. Even negative is not natural number. So as space that in writing it is nothing. Just an empty gap between word. But in context of computer it must have value So would you choose to have In some language like js there are concept of |
Beta Was this translation helpful? Give feedback.
-
Boy (girl), what a disussion... 😜 I think the discussion should just be about whether we can treat I myself have a discussion on dotnet/roslyn#9834 about allowing the void AMethod(void) { }
var t = Task.Run( (void) => { /* ... */ } ); The last line currently being var t = Task.Run( () => { /* ... */ } ); So On a logical side I totally see what @Thaina wants to achieve. return Console.WriteLine("We are leaving the procedure now"); my stomach turns 😧 It's just my guts... |
Beta Was this translation helpful? Give feedback.
-
@lachbaer Well... I just don't felt anything because that is valid in some other language like C++ and js http://stackoverflow.com/questions/3383090/is-returning-void-valid-code |
Beta Was this translation helpful? Give feedback.
-
Folks, let's look at the problem from another perspective. You can define your own [StructLayout(LayoutKind.Sequential, Size = 0)]
public struct Void { } Now you are allowed to use this type anywhere you want - you can return it as a result of a function, you can use it as a generic parameter. What it means to use such type is a different question, but you are allowed to do so, and the runtime does not break.
Please see another proposal where relaxing rules on P.S. the namespace System
{
/// <summary>Specifies a return value type for a method that does not return a value.</summary>
/// <filterpriority>2</filterpriority>
[__DynamicallyInvokable, ComVisible(true)]
[Serializable]
[StructLayout(LayoutKind.Sequential, Size = 1)]
public struct Void { }
} So it's either a bug in the definition, or |
Beta Was this translation helpful? Give feedback.
-
Why not something like: if (credentials.Code == Codes.BadCredentials) vreturn Console.WriteLine("Bad Credentials !'); instead of: if (credentials.Code == Codes.BadCredentials)
{
Console.WriteLine("Bad Credentials");
return;
}
There are plenty of cases where this keyword would improve code readability & maintenance 👍 |
Beta Was this translation helpful? Give feedback.
-
@Akronae I'd prefer |
Beta Was this translation helpful? Give feedback.
-
So we might go with something like |
Beta Was this translation helpful? Give feedback.
-
@Akronae |
Beta Was this translation helpful? Give feedback.
-
I know but it could be a way to implicitly tell the compiler "I want you to return void" and if the conversation fail (null) then the compiler throw an error. |
Beta Was this translation helpful? Give feedback.
-
@Akronae conversion from null to void is not an error. returning void is not really return anything. Just return from current stack to upper stack, same as There are mention about conversion because some function can return state as result, and it should be casted to void This proposal just propose syntactic sugar for one line returning but not changing any behaviour |
Beta Was this translation helpful? Give feedback.
-
Your supposed syntax would not even be legal outside of a void (or Quoting myself from a related thread:
Let's stop trying to "enhance" |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr We don't really want Your |
Beta Was this translation helpful? Give feedback.
-
Hi, the async Task SomeMiddlewareInvoke()
{
/* ... */
if (somethingWentWrong)
return CleanUpAndTerminate();
/* ... */
}
Task CleanUpAndTerminate()
{
/* ... */
} instead of async Task SomeMiddlewareInvoke()
{
/* ... */
if (somethingWentWrong)
{
await CleanUpAndTerminate();
return;
}
/* ... */
} |
Beta Was this translation helpful? Give feedback.
-
@MBeran To me, not-really-returning a Task instance seems just as weird as not-really-returning void. You can't think of this as actually returning the Task instance returned by CleanUpAndTerminate. |
Beta Was this translation helpful? Give feedback.
-
I'd like to point out that we can now return void in C#, but only in a limited fashion. But given that the step was taken in that direction, I believe there are grounds to make it a full feature. private string GetValue(int someValue) => someValue.ToString();
private void WriteToConsole(string val) => Console.WriteLine(val); I realize there is some syntactic sugar there. But if that's the argument, then just make syntactic sugar work for this: private async Task LogAndReturn(string val) {
var logVal = string.Format("{0}, lol", val);
await Log.DoStuff(logVal);
}
private async Task<T> LogAndReturn<T>(string val, T value) {
var logVal = string.Format("{0}, lol", val);
await Log.DoStuff(logVal);
return returnValue;
}
public async Task ExecuteTaskLol(parameters . . .) {
if (SomeValidation(param1) == false)
return await LogAndReturn(param1); // <-- This should work vvvv
DoMoarExecute(parameters . . .);
}
public async Task<bool> ExecuteTypedLol(parameters . . .) {
if (SomeValidation(param1) == false)
return await LogAndReturn(param1, false); // <-- The same as this ^^^
DoMoarExecute(parameters . . .);
} And though that example is async, it should also work just as well without async: private void LogAndReturn(string val) {
var logVal = string.Format("{0}, lol", val);
Log.DoStuff(logVal);
}
private T LogAndReturn<T>(string val, T value) {
var logVal = string.Format("{0}, lol", val);
Log.DoStuff(logVal);
return returnValue;
}
public void ExecuteTaskLol(parameters . . .) {
if (SomeValidation(param1) == false)
return LogAndReturn(param1); // <-- This should work vvvv
DoMoarExecute(parameters . . .);
}
public boolExecuteTypedLol(parameters . . .) {
if (SomeValidation(param1) == false)
return LogAndReturn(param1, false); // <-- The same as this ^^^
DoMoarExecute(parameters . . .);
} |
Beta Was this translation helpful? Give feedback.
-
I see no benefit to this. The reality is that Also the examples you give imply the If nothing is being returned the grammar should not pretend or imply that something is being returned. So I'd leave this well alone, it is what it is. |
Beta Was this translation helpful? Give feedback.
-
I still see no problem with this. My preference is that it be taken further (like I mentioned with switch Expressions). Having void be more like a unit type that can flow naturally seems sensible. Low priority on this though. |
Beta Was this translation helpful? Give feedback.
-
If this is so controversial, how about adding a new keyword? void LoadAndDoSomething(string url)
{
var obj = Load(url);
if(obj == null)
returnafter Console.WriteLine("Not found " + url);
// Do Something
} This has added benefit of being able to use methods that actually return something. Because if you would call: void LoadAndDoSomething(string url)
{
var obj = Load(url);
if(obj == null)
return url.ToString();
// Do Something
} I would expect compiler to give me an exception if a valid cast does not exist since you are trying to return a string as a void. But with: void LoadAndDoSomething(string url)
{
var obj = Load(url);
if(obj == null)
returnafter url.ToString();
// Do Something
} you don't imply you intend to return anything. Seems hacky, and adding an additional keyword just to handle some edge case seems a bit much. Nonetheless I'm just curious what you think. |
Beta Was this translation helpful? Give feedback.
-
In some condition we would do short logic and return from function immediately
So I think we should allow to return from void function with another void
Should also work with
Task
There are suggestion that we should be able to explicit cast
(void)
to be able to return function with any return typeThis was ported from dotnet/roslyn#11330
Alternative
guard statement
#138I was propose it should use
return if
syntax. Which would be more generalizeBeta Was this translation helpful? Give feedback.
All reactions