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: Volatile or Macro class with Dynamic base class #7458

Closed
neurospeech opened this issue Dec 13, 2015 · 3 comments
Closed

Proposal: Volatile or Macro class with Dynamic base class #7458

neurospeech opened this issue Dec 13, 2015 · 3 comments

Comments

@neurospeech
Copy link

The idea seems little strange, but there are various practical uses. Most of the time we implement same logic in many classes just because they are inherited from different classes. Let's assume a simple UI related logic, I want to write a OnClick tracking for couple of classes (I know, this can be done with event handlers, but its not the point, there may be classes without event handlers, non UI classes). They are TextBox, ComboBox, ListBox. Now all these classes already exist in framework. I have to create MyTextBox, MyComboBox and MyListBox.

And in each class I write same logic, ... track onclick. I create a ClickTracker class and create instance of ClickTracker as follow,

public class ClickTracker<T> where T: UIElement{
     public void OnClick(T sender, EventArgs e) {
        // my big implementation 
     }
}

public class MyTextBox: TextBox{
     ClickTracker<TextBox> tracker = new ClickTracker<TextBox>();
     override void OnClick(Object sender, EventArgs e){
           tracker.OnClick(this,e);
     }
}


public class MyComboBox: ComboBox{
     ClickTracker<ComboBox> tracker = new ClickTracker<ComboBox>();
     override void OnClick(Object sender, EventArgs e){
           tracker.OnClick(this,e);
     }
}

Not only implementation is time consuming but there is already lot of parameter passing.

If there was a way like...

// this class is volatile and can't be used without strict
// base class and it is just a placeholder, this is not a class and
// does not show up in reflection...
public volatile class ClickTracker : Control {

    public override void OnClick(Object sender, EventArgs e){
        // my tracking logic... 
    }

}


// this simply causes easy inline expansion of ClickTracker class
// look at implementation closely, MyTextBox is actually inherited from TextBox
public class MyTextBox: ClickTracker : TextBox{
}

public class MyComboBox : ClickTracker : ComboBox {
}

or we can have some alternative syntax like

public class MyTextBox: ClickTracker<TextBox>{
}

public class MyComboBox : ClickTracker<ComboBox> {
}

or

public class MyTextBox: ClickTracker(TextBox){
}

public class MyComboBox : ClickTracker(ComboBox) {
}

Volatile class properties

  1. It does not appear in reflection
  2. They are simply inline expansion like Macro
  3. Implementation of volatile class requires a non volatile base class
  4. Volatile class does not break single inheritance restriction
  5. Reduces unnecessary shim classes
@svick
Copy link
Contributor

svick commented Dec 13, 2015

I think this sounds a lot like #60.

@neurospeech
Copy link
Author

Trait is different, Volatile class derives from a specific base class, allowing it to easily access all members for editing and intellisense as well as it does not allow any random base class.

@neurospeech neurospeech changed the title Volatile or Macro class with Dynamic base class Proposal: Volatile or Macro class with Dynamic base class Feb 19, 2016
@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.

@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

2 participants