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

[Local Functions] Return from "parent" method #15288

Closed
Unknown6656 opened this issue Nov 16, 2016 · 3 comments
Closed

[Local Functions] Return from "parent" method #15288

Unknown6656 opened this issue Nov 16, 2016 · 3 comments

Comments

@Unknown6656
Copy link

Please do excuse me, if I am not up-to-date concerning the local functions-feature, but I have the following question/proposal:

Should it be possible, to allow a local function to return from the "parent" function?
I mean something along these lines:

public void Process( ... )
{
	SomeType obj1 = ...;
	SomeType obj2 = ...; // some resource-intensive objects

	void cleanup()
	{
		// do some object clean up
		obj1.Close();
		obj2.CleanUp();
		obj2.Dispose();

		return return; // return from "parent"
	}


	// main logic begins here
	...

	if (!some_condition_1)
		cleanup();
	...

	if (!some_condition_2)
		cleanup();
	...
}

which would be identical to:

public void Process( ... )
{
	SomeType obj1 = ...;
	SomeType obj2 = ...; // some resource-intensive objects

	void cleanup()
	{
		// do some object clean up
		obj1.Close();
		obj2.CleanUp();
		obj2.Dispose();
	}


	// main logic begins here
	...

	if (!some_condition_1)
	{
		cleanup();

		return;
	}
	...

	if (!some_condition_2)
	{
		cleanup();

		return;
	}
	...
}

As you can see, the statement return return; would not only return from the local function, but also from the parent one. (the syntax or keyword does not really matter to me --- it is more about the point of being able to emit control-statements to the parent function)

@HaloFour
Copy link

This has come up before:

#259 (comment)

@agocke
Copy link
Member

agocke commented Nov 17, 2016

Yup, Neal's comment is still relevant: local functions are lowered into their own methods, so they can't interact with the parent method except through the standard inter-method control flow (e.g., returning to the caller, yielding to the caller, etc.).

Since we're closing down for language features now I'm going to say that even if we liked this feature we wouldn't have time to put it into C# 7.

However, if you really want this feature and want it in a future version of C#, you could make a proposal for language design. For now I'll close this issue, though.

@agocke agocke closed this as completed Nov 17, 2016
@CodingOctocat
Copy link

Exception catching can be used:

private void IPConfigListCvs_Filter(object sender, FilterEventArgs e)
{
    var item = (IPConfigModel)e.Item;
    string keyword = SearchKeyword.ToLower();

    void Contains(Func<IPConfigModel, string> property)
    {
        if (property(item).ToLower().Contains(keyword))
        {
            e.Accepted = true;

            throw new Exception("return");
        }
    }

    try
    {
        Contains(x => x.Title);
        Contains(x => x.IPv4Config.IP);
        Contains(x => x.IPv4Config.Mask);
        Contains(x => x.IPv4Config.Gateway);
        Contains(x => x.IPv4Config.Dns1);
        Contains(x => x.IPv4Config.Dns2);
        Contains(x => x.Remark);
    }
    catch (Exception ex) when (ex.Message == "return")
    {
        return;
    }
}

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

5 participants