Is there an interface in C # for values ​​with interval scaling?

I am doing an extension to the interval set of the famous C # C5 library. The IInterval interface defines the interval with comparable endpoints (inactive members deleted):

 public interface IInterval<T> where T : IComparable<T> { T Low { get; } T High { get; } } 

This works well overall, since spaced endpoints can be comparable as integers, dates, or even strings.

However, sometimes it is useful to calculate the duration of the interval. The interval [3:5) has a duration of 2, and the interval [1PM, 9PM) has a duration of 8 hours. This is not possible for comparable data, since it gives us only the order of the elements, and not their distance, for example. it is difficult to give the distance between two strings. The endpoint type should mainly be interval scaling values .

Is there an interface like IComparable<T> that allows me to compare endpoints in general, but also do things like subtract two endpoints to get a duration and add duration to a lower endpoint to get a high endpoint, which can be used to inherit the IDurationInterval<T> : IInterval<T> interface, for example?

Or more concise: is there an interface for interval scaling values?

+7
c # intervals icomparable c5
source share
1 answer

There is no such interface. Some languages, such as Scala, have abstractions on these kinds of operations, but .NET does not. In addition, I found that the developers of the .NET library are quite conservative when it comes to adding levels of abstraction: they seem to prefer a simple and concrete complex and abstract. They never added this abstraction, presumably because any of the .NET Framework libraries was never an absolute necessity. It is also an abstraction that can cause a lot of overhead when misused.

However, nothing in a system like .NET allows such an interface. This will require two type parameters instead of one: one for the implementation type and one of the result type. The most basic interface might look like this:

 interface IAlgebraic<T, R> { T Add(R value) R Subtract(T value) } 

Unfortunately, there is no way to add this interface to existing types such as Int32 and DateTime. For these types, you will need the IComparable<T> Comparer<T> corollary.

 interface IAlgebraicOps<T, R> { T Add(T x, R y) R Subtract(T x, T y) } 
+4
source share

All Articles