Intro
This question prompted by Marc Gravell's suggestion that I post new language feature suggestions to this site to gather general opinion on them.
The idea is to gather if they might be helpful or maybe there is already another way to achieve what I am after.
Suggestion (Constrained Types)
A normal variable declaration in VB.Net is written thus:
Dim SomeVariable as SomeType
I suggest allowing the following form(s)
Dim SomeVariable1 as {SomeType, ISomeInterface}
Dim SomeVariable2 as {SomeType, ISomeInterface, ISomeOtherInterface}
This syntax is borrowed from Vb.Net's style of constraining Generics
Why allow this?...What is it good for?
Well the specific case I originally thought of was to define a particular subset of controls. I wished to create an interface for a series of Control factories which would provide controls based on some business rules.
The consumer of these controls would require, through the interface, that all controls created, should also implement some series of interfaces (only one in my case) which gave all these controls additional facilities not found generally in normal controls.
It is worth noting that the following currently does not work.
Public Interface ISpecialControl
End Interface
Public Interface IControlProvider
Function CreateControl(Of T As {Control, ISpecialControl})() As T
End Interface
Public Class SpecialTextBoxProvider
Implements IControlProvider
Public Function CreateControl(Of T As {Control, ISpecialControl})() As T Implements IControlProvider.CreateControl
Return New SpecialTextBox
End Function
End Class
Public Class SpecialTextBox
Inherits TextBox
Implements ISpecialControl
Public Sub New()
End Sub
End Class
I think this translates to C# as:
public interface ISpecialControl
{
}
public interface IControlProvider
{
T CreateControl<T>()
where T : Control, ISpecialControl;
}
public class SpecialTextBoxProvider : IControlProvider
{
public T CreateControl<T>()
where T : Control, ISpecialControl
{
return new SpecialTextBox();
}
}
public class SpecialTextBox : TextBox, ISpecialControl
{
}
The attempt to return "New SpecialTextbox" fails due to an inability to cast SpecialTextbox to T.
"Value of type 'MyApp.SpecialTextBox' cannot be converted to 'T'"
I realize that my factories could be allowed to return simple controls and I could check at runtime if they implemented ISpecialControl, but this would yield runtime problems which I would rather check at compile time since it is a logical possibility even if not currently a practical one
Update: The idea is that these factories could sit in external (perhaps even third party) assemblies and could take a dependency on any control libraries they wanted, creating and returning derivatives of these controls which also implemented ISpecialControl.
These assemblies could be located through self-configuring-reflection (Reflection on first pass followed by configuration, which is then used on further runs) and used without any knowledge by the calling assembly on what dependancy these controls would have taken.
It does require that these factories are constructable without passing information about the control they are expected to call, since that would defeat the point.
So what do you think... Would this be useful?... Is there a better way to achieve this? Is there already a way to achieve this?