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] Deligation class #1461

Closed
momijin opened this issue Mar 21, 2015 · 3 comments
Closed

[Proposal] Deligation class #1461

momijin opened this issue Mar 21, 2015 · 3 comments

Comments

@momijin
Copy link

momijin commented Mar 21, 2015

Problem

To use the delegation on various design patterns, we must write a lot of tedious code.

Proposal

To introduce the delegation class in c#.

Example

Class Z use a delegation to class A.

class A
{
    public int Method1() { return 0; }
    public void Method2(int p) {}
    public void Method3(int p) {}
}

class Z : delegate A
{
    public void Set(A a) { base<A> = a; }
}

The delegation syntax is written like the inheritance syntax;
Above is expanded to:

class Z
{
    private A _a;
    public int Method1() { return _a.Method1(); }
    public void Method2(int p) { _a.Method2(p); }
    public void Method3(int p) { _a.Method3(p); }

    public void Set(A a) { _a = a; }
}

Three methods are automatically generated by the compiler.

Next example is using with readonly and override keyword. You must set a delegation in constructor and you can change a method behavior.

class Y : delegate readonly A
{
    public Y(A a) { base<A> = a; }
    public override int Method1() { return base<A>.Method1() + 1; }
}

Above is expanded to:

class Y
{
    private readonly A _a;
    void Method2(int p) { _a.Method2(p); }
    public void Method3(int p) { _a.Method3(p); }

    public Y(A a) { _a = a; }
    public int Method1() { return _a.Method1() + 1; }
}

Next example is using two delegation classes. Class B has two conflict method names, so one is given an alias name, other is excluded.

class B
{
    public int Method1() { return 10; }
    public void Method2(int p) {}
    public void Method3(string q) {}
}

class W : delegate A, delegate B
{
    uses
    {
        B { alias B_Method1 = Method1; exclude Method2; }   
    }
    public void Set(A a) { base<A> = a; }
    public void Set(B b) { base<B> = b; }
}

Above is expanded to:

class W
{
    private A _a;
    public int Method1() { return _a.Method1(); }
    public void Method2(int p) { _a.Method2(p); }
    public void Method3(int p) { _a.Method3(p); }

    private B _b;
    public int B_Method1() { return _b.Method1(); }
    public void Method3(string q) { _b.Method3(q); }

    public void Set(A a) { _a = a; }
    public void Set(B b) { _b = b; }
}

Next example is using with inheritance. It is similar to the previous example.

class U : A, delegate B
{
    uses
    {
        B { alias B_Method1 = Method1; exclude Method2; }   
    }
    public void Set(B b) { base<B> = b; }
}

Above is expanded to:

class U : A
{
    private B _b;
    public int B_Method1() { return _b.Method1(); }
    public void Method3(string q) { _b.Method3(q); }

    public void Set(B b) { _b = b; }
}

I wish that this proposal makes C# better and it is useful for the people using C#.

@zgxnet
Copy link

zgxnet commented Mar 23, 2015

This is actually a mixin.
See #60
I think a better way is to also support mixin in generic constraints.

@momijin
Copy link
Author

momijin commented Mar 23, 2015

@zgxnet

I read #60 interestingly very much. I wish that C# 7 will be added a mixin or a trait that has better supports of delegation.

Thank you for your kind comment.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

Traits, which are proposed at dotnet/csharplang#52, can provide some of the requestion functionality but in a syntactically different form.

@gafter gafter closed this as completed Mar 20, 2017
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