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

[Discussion] More statements as expressions. #5882

Closed
orthoxerox opened this issue Oct 12, 2015 · 2 comments
Closed

[Discussion] More statements as expressions. #5882

orthoxerox opened this issue Oct 12, 2015 · 2 comments

Comments

@orthoxerox
Copy link
Contributor

Since this was briefly mentioned in #5154 I've decided to create a separate issue for this discussion.

1. Blocks inside method bodies are expressions and return the value of their last expression.

let x = {
    let a = 1;
    let b = 2;
    a + b;
}

2. You cannot end a block inside a method body with a declaration or a statement that isn't guaranteed to not return if its return value is used.

let x = {
    let a = 1;
    let b = 2;
    a + b;
    int y; //I won't compile!
}

3. if is now an expression and accepts expressions as its two branches and returns the value of the one that is executed. When the return value of if is used its else branch becomes mandatory.

let x =
    if (list != null && list.Count > 0) {
        list[list.Count - 1];
    } else {
        getSomeDefaultValue();
    }

let y =
    if (list != null && list.Count > 0) {
        list[0];
    } //I won't compile!

4. for, foreach, while and do ... while are not expressions (because their body can be executed zero times).

5. try and its derivatives are now expressions and return the value of the try block when no exception is thrown and the value of a catch block when an exception is caught by one. The value of the finally block is discarded.

let result =
    try {
        GetResult();
    }
    catch (Exception ex) {
        logger.Log(ex);
        GetSomeDefaultResult();
    }
    finally {
        Cleanup();
    }
return lock(lockField)
    this.blahField;

6. switch is not an expression (because there's match.)

7. You cannot yield a value of a non-final expression in a block.

@alrz
Copy link
Member

alrz commented Oct 12, 2015

basically, #5402

@orthoxerox
Copy link
Contributor Author

Duplicate of #5402

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

3 participants