when implementing GenericClass<T> be able to declare additional constraints for T on some of its methods #3469
Replies: 4 comments
-
Yes, I am aware of it, but it is just a "workaround" that pattern won't allow me to create something that inherits from ObjectWithLock<>. Mine just is an example of a possible application, it hasn't necessarily to be the object constructor the one to be constrained Recently I wrote a control<->model binding mechanism for Xamarin android that allows me (for example) to bind a textview control to a string variable in the model like in this example
I wanted it to be totally idiot-proof: I built it in order to make it impossible to call MinValue() if the type of the property being bound isn't actually an IComparable value I am pretty sure that if that feature was in place, I would have done it defining 1/3 of all the interfaces I had to write... |
Beta Was this translation helpful? Give feedback.
-
There seem to be a lot of issues related to "generic specialization": #662, #1012, #1315. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt It's not a solution since
There is also some minor problem
And yes a very old issue. I remember 4 year ago there already was an issue about it on github. I try to search it but i cannot found it... I'm not sure it was in the csharplang repo... The solution today could be : class Base<T>
where T:class {
protected T myField;
protected MySpecificMethod(IEnumerable myParameter){ ... };
}
class Specific<T> : Base<T>
where T:IEnumerable, Other {
protected MySpecificMethod(){ base.MySpecificMethod(myField) };
}
class Specific2ButWhoAlsoNeedMySpecificMethod<T> : Base<T>
where T:IEnumerable, OTher2 {
protected MySpecificMethod(){ base.MySpecificMethod(myField) };
} But if you want to ask why in my exemple i didn't create |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt Sorry i was talking about static extension because i know it's the common answer. But i forget to say that any thinks i say is also true for simple static method. Different but exactly the same in the end. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Maybe an example is worth more than 100 words..
to give you some context:
I am implementing a "ObjectWithLock" class which is a class that will wrap an instance of T and will allow access to such instance only by calling its Lock() method.
The thing I am writing will be used like this:
I'd like my class to have also a parameterless constructor that autonomously creates the instance of List: i don't want to be forced to create externally the instance to be locked.
Now: currently the only way of doing this in a type-safe way is declaring my class like this
What I'd like is to be able to write this:
(and I'd like to add such kind of constraint also to other methods)
Conceptually it could make sense. When you instantiate the generic type, you just leave out, in the instantiated class, the methods that don't meet the constraints for the given type.
I know... this is easier to be said than done, but it could be implemented by transparently creating a new generic type that inherits from ObjectWithLock, adds the new constraint at class declaration level and adds the new constrained methods (that the compiler leaves out in the base unconstrained generic class)
Beta Was this translation helpful? Give feedback.
All reactions