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

Discussion/Suggestion: Type matching with generic constraints #619

Closed
weitzhandler opened this issue May 24, 2017 · 2 comments
Closed

Discussion/Suggestion: Type matching with generic constraints #619

weitzhandler opened this issue May 24, 2017 · 2 comments

Comments

@weitzhandler
Copy link

weitzhandler commented May 24, 2017

Hi,

I came across a funny issue today, where I have an object of unknown type, and it's composed of several interfaces without having a concrete type or connection between them, to be passed as a single type. Let the code do the talking:

foreach(PropertyInfo property in GetProperties()
{ 
  var value = property.GetValue(obj);
    if(value is Blah)
    {
    }
    else if (value is INotifyCollectionChanged incc
      && value is IEnumerable<ITrackableObject> trackableCollection)
    /*****************************************************/


 //1
 void ProcessTrackableCollection<TCollection>(TCollection collection)
    where TCollection : INotifyCollectionChanged, IEnumerable<ITrackableObject> 
 {
   //perfect now how r u gonna call it without having a concrete type
 }
 //2
 void ProcessTrackableCollection(object collection)
 {
   //meh
   var incc = (INotifyCollectionChanged)collection;
   var tCol = (IEnumerable<ITrackableCollection>)collection;
 }
 //3
 void ProcessTrackableCollection(INotifyCollectionChanged collection)
 {
    // IEnumerable<ITrackableCollection> will really cry
    var tCol = (IEnumerable<ITrackableCollection>)collection;
 }
 //4
 void ProcessTrackableCollection(INotifyCollectionChanged incc, IEnumerable<T> collection) 
 { /* :P */ }

I hope the situation is clear by the code itself, so my suggestion is to be able to define aliases of composed type constraints and use them wherever:

using TCollection : INotifyCollectionChanged, IEnumerable<ITrackableObject>;
namesapace Hello
{

  public class Program
  {
    
    static void Main()
    {
       PropertyInfo property = GetPropertyInfo();
       object obj = (TCollection)property.GetValue(GetItem());
       Foo(obj);
    }

    public static Foo(TCollection obj) 
    {
       obj.CollectionChanged += OnCollectionChanged;
       _Items = obj.ToArray(); //no cast
    }       
  }
}

I hope I'm clear enough, should I hear rants and I'll expand.

@weitzhandler weitzhandler changed the title Type matching with Generic constraints Discussion/Suggestion: Type matching with Generic constraints May 24, 2017
@weitzhandler weitzhandler changed the title Discussion/Suggestion: Type matching with Generic constraints Discussion/Suggestion: Type matching with generic constraints May 24, 2017
@HaloFour
Copy link
Contributor

I believe that this falls under union/intersection types, which is not something that C# or the CLR supports (outside of generic type constraints). See #228, #344 and #399.

In short, you'd probably want something like:

if (value is (INotifyCollectionChanged & IEnumerable<ITrackableObject>) collection) {
    // collection treated as both interfaces here
}

This is assuming that I am interpreting your "discussion" correctly.

@weitzhandler
Copy link
Author

weitzhandler commented May 24, 2017

@HaloFour lol I know it was gross, looks like you understood me well, tho I was more about the aliasing part than the union part.
Thanks for the references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants