Skip to content
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

Proposal: Allow block-statements with return values everywhere an expression is expected #15821

Closed
YaakovDavis opened this issue Dec 10, 2016 · 5 comments

Comments

@YaakovDavis
Copy link

YaakovDavis commented Dec 10, 2016

I propose to allow the following syntax:

var x = {var foo = Foo(); return foo * 3;};

As another example, in ternary operators, this will look like:
var x = condition ? {var foo = Bar(); return foo + 1;} : 2;

Such a syntax increases the composability of the language, allowing one to express logic using a natural flow, while also limiting the scope of local intermediary variables.
F# has a similar syntax, allowing to define arbitrarily nested expressions.

@orthoxerox
Copy link
Contributor

#6182

@qrli
Copy link

qrli commented Dec 11, 2016

also related to #15651

@binki
Copy link

binki commented May 4, 2017

I think seeing return inside of a straight up block would be confusing with existing syntax. This code is valid syntax currently and causes a return from MyMethod:

static int MyMethod()
{
    var x = 2;
    {
        return x + 4;
    }
    // unreachable.
}

With this proposed syntax, you could do:

static in MyMethod()
{
    var x = 2;
    var y = {
        return x + 4;
    };
    // Suddenly this is reachable.
    return y;
}

In quickly visually scanning a method, it seems like the return in a “returnable block” is not visually distinguishable from a return in a nested block which causes the outer function to return. This issue is described here too.

@YaakovDavis
Copy link
Author

@binki

To me, the 2 snippets look visually distinct.
In any case, it has been proposed elsewhere to use the following syntax:

var y = ({
   return 4;
});

@CyrusNajmabadi
Copy link
Member

Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks!

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants