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] Local class declaration #280

Closed
zgxnet opened this issue Feb 6, 2015 · 8 comments
Closed

[Proposal] Local class declaration #280

zgxnet opened this issue Feb 6, 2015 · 8 comments
Assignees

Comments

@zgxnet
Copy link

zgxnet commented Feb 6, 2015

java supports local class declaration, e.g.
public void Method1(){
class Class1 {
}
}
This is super useful when a function logic is complex, and some data types are not used outside the function. Such local class just solves the problem.
Besides, C++ also supports that.

It is a pity that C# does not support that feature.
And I think this is just a syntax sugar, and does not involve the changes of CLR. So it is an easy feature.

In java, the local class is just treated as an inner class, e.g. MethodName$LocalClassName.
And C# can use the same strategy.

@mirhagk
Copy link

mirhagk commented Feb 6, 2015

C# does support anonymous local class delcarations, which I would think solves any of the use cases a local class would solve. Intellisense does already pick up the member names, and it is statically typed, and assignment between objects of the same type work, so this only gives you the chance to define constructors (methods could already be assigned with lambdas) and that would not be appropriate for a locally declared class anyways.

I can't see any use case that anonymous classes don't solve that this local class declaration does solve. Anonymous classes are much more elegant in that it doesn't require declaring and then using them, you can simply use them.

@AlgorithmsAreCool
Copy link

@mirhagk I can give an example, there have been times when i need to provide an IComparer to a method. But since anonymous classes can't have methods and can't implement interfaces, I have to declare a traditional class to do the job. I would be happy enough to have some syntax :

var myComparer = new IComparer<string> {
    int CompareTo(string a, string b ) { return a[0].CompareTo(b[0]) }
}

or using expression bodies

var myComparer = new IComparer<string> {
    int CompareTo(string a, string b) => a[0].CompareTo(b[0]);
}

@alanfo
Copy link

alanfo commented Feb 6, 2015

Local types have already been proposed below though, personally, I have my reservations about them:

Proposal: Nested local functions and type declarations #259

@mirhagk
Copy link

mirhagk commented Feb 6, 2015

@AlgorithmsAreCool Okay well I think the right route to go then would be to let anonymous classes declare methods (lambdas) and implement interfaces. That would be the most lightweight solution to the problem.

@zgxnet
Copy link
Author

zgxnet commented Feb 6, 2015

There are some other cases, especially in algorithm design.
Here is a usual pattern in dynamic programming.
int CalcBest(int[] data)
{
struct State
{
public int v0;
public int v1;
}
State[] d = new State[data.Length];
d[0].v1 = d[0].v0 = data[0];
for(int i=1;i<data.Length.i++)
{
d[i].v0 = somefunc1(d[i-1].v0,d[i-1].v1,data[i]);
d[i].v1 = somefunc2(d[i-1].v0,d[i-1].v1,data[i]);
}
return somefunc3(d[data.Length-1].v0, d[data.Length-1].v1);
}
I know Tuple may solve some problems, but if there are too many state variables, the readability will be very bad

@svick
Copy link
Contributor

svick commented Feb 6, 2015

@AlgorithmsAreCool I think the right solution for that specific case is to use a library that converts key selector to IComparer:

var comparer = KeyComparer<string>.OrderBy(s => s[0]);

@gafter gafter self-assigned this Feb 6, 2015
@zgxnet
Copy link
Author

zgxnet commented Feb 6, 2015

@svick I think there are many other interfaces that we want to implement anonymously and locally. Such a library solution is somewhat adhoc and may cause some performance issues. We cannot write a library for each of them.

@gafter gafter added Resolution-Duplicate The described behavior is tracked in another issue 4 - In Review A fix for the issue is submitted for review. Area-Language Design Feature Request Language-C# labels Feb 6, 2015
@gafter
Copy link
Member

gafter commented Feb 6, 2015

Duplicate of #259

@gafter gafter closed this as completed Feb 6, 2015
@gafter gafter removed the 4 - In Review A fix for the issue is submitted for review. label Feb 9, 2015
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