Replies: 50 comments 8 replies
-
Combined with #102, we could get really objective code, like this one: R M(object obj)
{
var c = obj as C ?? return default;
//...
} Which would be equivalent to the code below with current language specs: R M(object obj)
{
if(obj is C c)
{
//...
}
//else not needed since we suppose there is a return inside the if block
return default(R);
} Which is far more objective than what we had using c# 6.0: R M(object obj)
{
var c = obj as C;
if(c == null)
return default(R);
//...
} |
Beta Was this translation helpful? Give feedback.
-
This feature will encourage mixing imperative and functional programming styles. That can turn out to be a bad idea IMHO. |
Beta Was this translation helpful? Give feedback.
-
Not sure how is this functional but mixing imperative and functional programming were never considered a bad idea and in fact it's one of the fundamental design directions that C# follows. |
Beta Was this translation helpful? Give feedback.
-
This code contrasts among different coding styles- // Functional
var res1 = list
.TakeWhile(n => n != -1)
.Aggregate((i, j) => i + j);
// Imparetive
var res2 = 0;
foreach (var n in list)
{
if (n == -1) break;
res2 += n;
}
// Mixed, enabled by this proposal
var res3 = 0;
foreach (var n in list)
{
res3 += (n != -1) ? n : break;
} In functional style, everything is an expression. Where as in imperative style, everything is a statement. In functional languages loops are avoided and recursion is the way of repeating some task. While F# does allow loops, Newly added
|
Beta Was this translation helpful? Give feedback.
-
One of the things that I like about the potential of using var query = from n in list
select n == 1 ? "one"
: n == 2 ? "two"
: n == -1 ? break
: continue;
// translates into
var query = list.Select(n =>
n == 1 ? (true, true, "one")
: n == 2 ? (true, true, "two")
: n == -1 ? (false, true, default(string))
: (true, false, default(string)))
.TakeWhile(tuple => tuple.Item1)
.Where(tuple => tuple.Item2)
.Select(tuple => tuple.Item3); As for the argument regarding imperative v. functional programming, most developers don't think in such black and white. A functional feature in an imperative language isn't inherently a bad (or good) thing. And while it's true that not everything is an expression in imperative languages, many things still are. Beyond the idealistic argument, I can understand that it might be difficult to discern that an expression could affect the flow of the containing code if a control clause is buried deeply within. But I don't think I'd doom the entire proposal based on what a bad/"clever" programmer can do to abuse it. |
Beta Was this translation helpful? Give feedback.
-
Not oppose the idea just present what I normally did void M(object obj)
{
foreach (var item in list.Where((item) => item >= 1 && item <= 3).TakeWhile((it) => it != 3))
{
var result = item == 1 ? "one" : "two";
}
}
// Actually I would like to explicit if break from the first line
void M(object obj)
{
foreach (var item in list.Where((item) => item >= 1 && item <= 3))
{
if(item == 3)
break;
var result = item == 1 ? "one" : "two";
}
} Well, I think this feature has too many alternative |
Beta Was this translation helpful? Give feedback.
-
Proposal #135 could actually come in handy here, as well. |
Beta Was this translation helpful? Give feedback.
-
Since foreach (var item in list ?? return) I wonder if we could abuse foreach (var item in list ?? break) |
Beta Was this translation helpful? Give feedback.
-
I don't see where foreach (var item in list ?? return) {
...
}
// ** example is wrong ** just left for documentation
foreach (var item in list) {
if (item == null) return;
...
}
// not this
(var item in list) ?? return
or
// it's this
var item in (list ?? return) BTW, |
Beta Was this translation helpful? Give feedback.
-
Except that it will throw. You probably want to write it before the loop. Equivalent code of if (list != null)
{
foreach (var item in list)
{
}
} Embedded statements can make this less verbose but I would certainly prefer
foreach (var item in list ?? throw new Exception(..)) But it's useless, because you are just converting a NRE to another exception.
I don't disagree, but I just didn't find any sensible use case for it. Personally, I try to not use |
Beta Was this translation helpful? Give feedback.
-
Or foreach (var item in list ?? emptyList)
{
// ...
} |
Beta Was this translation helpful? Give feedback.
-
If |
Beta Was this translation helpful? Give feedback.
-
I don't use I like this proposal. C# has already come so far and there are already so many ways to reach the goal, including functional vs. imperative approaches, LINQ, wse. I think that there surely will be definite use cases for it where it will improve reading or the process of writing. There are many that prefer not to jump out of a block in the middle! That also is probably just a matter of taste and personal experience. I would recommend to specially syntax highlight the occurences of these keywords in code editors, whether standing allone or being part of a longer expression!!!! (There are already extensions, but it should be standard). |
Beta Was this translation helpful? Give feedback.
-
Just wanted to note, that |
Beta Was this translation helpful? Give feedback.
-
@lostmsu See the example in the OP with |
Beta Was this translation helpful? Give feedback.
-
Agreed, since we should correspond |
Beta Was this translation helpful? Give feedback.
-
Sounds good. Kind of a shame that it couldn't be used to skip the current enumeration. Maybe foreach (var foo in bar ?? goto label1) { ... }
label1:
label2: foreach (var foo in bar ?? break label2) { ... } |
Beta Was this translation helpful? Give feedback.
-
@HaloFour what's the difference between a break label, a continue label, and a goto? |
Beta Was this translation helpful? Give feedback.
-
Avoids the |
Beta Was this translation helpful? Give feedback.
-
There is always a possibility of future |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I totally didn't know about Java Branching Statements. Is there something preventing C# to have this kind of features? |
Beta Was this translation helpful? Give feedback.
-
Desire? Philosophy? C# has the feature, just with different syntax. The only reason to suggest implementing Java's syntax is disliking having to use the keyword |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
You're right. It can be accomplished through a combination of other things, such as |
Beta Was this translation helpful? Give feedback.
-
I'm carefully optimistic about this idea. I often find myself writing boilerplate code such as: var user = await userManager.GetUserAsync(User);
if (user == null) { return RedirectToLogin(); }
// or
var thing = await thingService.GetThingAsync(id);
if (thing == null) { return RedirectToIndex(); } Which, with this proposal, could be: var user = await userManager.GetUserAsync(User) ?? return RedirectToLogin();
// or
var thing = await thingService.GetThingAsync(id) ?? return RedirectToIndex(); I think it reads quite nicely, and the verbosity from what I have now is not helping here. |
Beta Was this translation helpful? Give feedback.
-
Is this proposal going on in another thread? I wonder why it is inactive despite the fact that it is one of the most useful features. After the addition of support for throwing exceptions in the null-coalescing operator, I was anticipating return statements to be added either. But after three years, it seems that it is not even one of the champions. |
Beta Was this translation helpful? Give feedback.
-
I have a different take on this proposal and swapped the syntax around. basically: so it's like the 'else if' statement
|
Beta Was this translation helpful? Give feedback.
-
This feature makes the code more succinct and its intentions are clear. What's not to like? |
Beta Was this translation helpful? Give feedback.
-
Folding labeled break/continue (#6634) into a bucket with this may make improve the chances of these being done. |
Beta Was this translation helpful? Give feedback.
-
I agree. Another example in a method with void return type: var x = n switch (Should I start another 'discussion' to make this a 'proposal'? Is there already one out there?) |
Beta Was this translation helpful? Give feedback.
-
Ported from dotnet/roslyn#14239
return, break and continue expressions
Summary
Lets you use
return
,break
andcontinue
as an expression.Proposal
Since
throw
is now an expression, I thinkreturn
,break
andcontinue
are also good candidates to be an expression.There is no straightforward alternative for the third example as you will need to use the
switch
statement.Open question:
throw
expressions are not allowed as the RHS for&&
and||
. should that also be applied here?Unlikethrow
, these expressions need to return a value,EDIT: We could make this specific example an error as suggested by @lostmsu.
Open question: should we allow
yield break
andgoto
as an expression? (yield return
is excluded because it is not a jump instruction).Beta Was this translation helpful? Give feedback.
All reactions