[This post is part of a series, "wish-list for future versions of VB"]

 

IDEA: Allow shared methods inside interfaces, including operators. To support this we’d also need changes inside the CLR.

SCENARIO: You want to write a general-purpose mathematical algorithm that operates upon any numeric type. You’d like to write it like this:

Interface INumeric

    Shared Operator +(ByVal x As INumeric, ByVal y As INumeric) As INumeric

End Interface

 

Class Integer : Implements INumeric

End Class

 

Class Double : Implements INumeric

End Class

 

Function FourierTransform(Of T As INumeric)(ByVal x As T()) As T()

    ' ...

End Function

The current workaround is that you have to provide several different versions of your function, one for each numeric type that you want it to work upon.

 

SCENARIO: Template meta-programming. I have a hunch that shared methods in interfaces would open up a whole new world of programming, not as powerful as C++ template meta-programming, but getting close. This is just a hunch so far. If you can think of powerful subversive uses of shared methods in interfaces, please write in!

 

Note that the “generic numeric library” doesn’t seem particularly compelling. That’s because maths libraries like this are supposed to be super-fast, and the JIT can do a better job of optimizing if it’s given specialized versions of each function.

 

Provisional evaluation from VB team: We do not think this idea is worth pursuing. (As always, we're open to persuasion if you come up with a nicer design or more compelling scenarios!)