Add interfaces for primitives #2166
Replies: 19 comments
-
If those interfaces don't have any members, how are you going to implement that logic? Say, you wanted to be able to add two If you mean that this would be only for types that don't have any members that operate with the |
Beta Was this translation helpful? Give feedback.
-
A better idea of a "static interface" has been floated in the past, which would allow defining static methods and operators. Not sure where that is, there's probably an issue for it still. |
Beta Was this translation helpful? Give feedback.
-
You might be thinking of Shapes, which sounds like what OP is suggesting here. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
What I meant is, to have a common interfaces between primitive types, just as an example, let's take vectors. Why should I make a so many vector structs when I could have a generic one? which currently ins't possible unless you make conversions and a load of methods, for example, a simple public interface INumeric<T> where T : INumeric
{
const T MaxValue;
const T MinValue;
} Another possibility would be to make
Not exactly but my suggestion is related to it somehow, the idea of Shapes is really interesting In general, my suggestion would look kind of like this (assuming the possibility of static/const members on interfaces): public interface IPrimitive : IComparable, IConvertible, IFormattable, ISpanFormattable { }
public interface IPrimitive<T> : IPrimitive, IComparable<T>, IEquatable<T>
{
static T Parse(string s);
static T Parse(string s, IFormatProvider provider);
static bool TryParse(string s, out T result);
static bool TryParse(string s, IFormatProvider provider, out T result);
}
public interface INumeric : IPrimitive { }
public interface INumeric<T> where T : INumeric
{
const T MinValue;
const T MaxValue;
static T Parse(string s, NumberStyle numberStyle);
static bool TryParse(string s, NumberStyle numberStyle, out T result);
} you may get an idea from here I guess |
Beta Was this translation helpful? Give feedback.
-
Writing an algorithm dependent on vectoring everything through
Something like the Shapes proposal (#164) will be better performant. |
Beta Was this translation helpful? Give feedback.
-
Not if the interface was used as a generic constraint. Then every instance of the generic type would get specialised, as the numeric primitives are all value types. The performance is equavilent to that produced by CodeGen. |
Beta Was this translation helpful? Give feedback.
-
Interestingly, something like this must have been seriously investigated at some point: https://github.com/Microsoft/referencesource/search?q=iarithmetic&unscoped_q=iarithmetic |
Beta Was this translation helpful? Give feedback.
-
Personally I would just want this as a generic constraint. E.g, |
Beta Was this translation helpful? Give feedback.
-
That'd work too, but here should be more constrains, like for numeric and non-numeric (date and string/char) |
Beta Was this translation helpful? Give feedback.
-
Neither DateTime nor String is primitive |
Beta Was this translation helpful? Give feedback.
-
Oh I see, decimal isn't a primitive either so I guess numeric constrain would be better? |
Beta Was this translation helpful? Give feedback.
-
I guess, though a primitive constraint might still have uses |
Beta Was this translation helpful? Give feedback.
-
@johnkellyoxford
What now? :D |
Beta Was this translation helpful? Give feedback.
-
Neither static nor const members make sense on interfaces. The former requires an instance through which to perform a vtable lookup in order to implement a virtual dispatch. The latter is not a proper member and requires that the compiler resolve and replace with the actual value at compile time. I'd think all of the functionality that you're looking for can be accomplished through shapes, which will be significantly more flexible and performant than normal interfaces could be in these circumstances. |
Beta Was this translation helpful? Give feedback.
-
I feel like this is quite close to the Shapes and Extensions proposal:
Both of these are precisely the example given by Mads: first, he defines a contract From my understanding, in a world where Since you said "not exactly" — semantics aside, is there something in particular about your proposal that differs? *) I'm avoiding the word interface or shape in favor of something more all-encompassing. |
Beta Was this translation helpful? Give feedback.
-
Although, I haven't benchmarked to see if it is poor performant as people claims. |
Beta Was this translation helpful? Give feedback.
-
@JustNrik while that looks horrifying in C#, it's clearly very well optimised by the JIT to remove all the casts, so I wouldn't think it was poor performing |
Beta Was this translation helpful? Give feedback.
-
I'm not sure what that static class example has to do with your proposal. The shapes proposals handle specialization in a different way, through witness structs that implement the shape "interface". The witness structs would be automatically generated by the compiler and would wire up the instance and static members of the target instance to the members of the shape: This example might be more inline with the original shapes proposal: Of course the design of shapes/roles/extensions/etc is very up in the air, but of the motivations seems to be making this kind of programming possible and efficient. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My suggestion is to add the following interfaces
Although they don't have any member to implement (pretty much "cosmetic" interfaces) they would be useful for generic constrains. For example:
So we could use it as:
instead of creating 3 different structs with pretty much the same logic
What do you think guys?
Beta Was this translation helpful? Give feedback.
All reactions