anthony
anthony

Reputation: 41118

What's the opposite of the term "closed over"?

Consider the following (C#) code. The lambda being passed to ConvolutedRand() is said to be "closed over" the variable named format. What term would you use to describe how the variable random is used within MyMethod()?

void MyMethod
{
    int random;
    string format = "The number {0} inside the lambda scope";

    ConvolutedRand(x =>
    {
        Console.WriteLine(format, x);
        random = x;
    });

    Console.WriteLine("The number is {0} outside the lambda scope", random);
}

void ConvolutedRand(Action<int> action)
{
    int random = new Random.Next();
    action(random);
}

Upvotes: 1

Views: 125

Answers (2)

Brian
Brian

Reputation: 118895

I typically hear "bound" versus "free", in the context of a particular expression or lexical scope. The lambda closes over both format and random (which are 'free' in the lambda, which is why it closes over them). Inside MyMethod, both variables are just locally bound variables.

Upvotes: 3

leppie
leppie

Reputation: 117280

That would be a local variable IMO. (Perhaps there is a more scientific name, not free maybe?)

Upvotes: 1

Related Questions