Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Circular function expressions #610

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

Merged
merged 2 commits into from
Sep 5, 2014
Merged

Conversation

ahejlsberg
Copy link
Member

Fixes #445 and #583.

Function expression bodies are now checked in a separate phase after all statements in the enclosing body to ensure constructs like the following are correctly typed:

var foo = function () {
    var s = foo();
    return "hello";
}

Previously we'd perform a full type check of the body of the function expression whilst in the process determining the type of foo which would cause foo to be given type any because of the recursive reference. With this change, foo is now correctly typed as () => string.

@@ -7,6 +7,8 @@

// 'private' should not be considered a member variable here.
private[key] = value;
~~~~~~~
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is interesting.. why was that missed before?
also the comment seems wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this error wouldn't be reported because the expression in the return statement isn't type checked if the return statement occurs in a disallowed location. It's not worth the complexity to recompute whether we should skip the function expression body check phase, so the check always occurs.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 5, 2014

👍

1 similar comment
@vladima
Copy link
Contributor

vladima commented Sep 5, 2014

👍

ahejlsberg added a commit that referenced this pull request Sep 5, 2014
@ahejlsberg ahejlsberg merged commit e157763 into master Sep 5, 2014
@ahejlsberg ahejlsberg deleted the circularFunctionExpressions branch September 5, 2014 22:45
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type of variable incorrectly any when used in function body in its own initializer
4 participants