Replies: 17 comments
-
|
Beta Was this translation helpful? Give feedback.
-
In the world of generics it's actually a little weirder than that. Just the constraint // assume the compiler allowed this today, without a special syntax
public class C1<T> where T : System.Enum { }
public class C2<T> where T : System.Enum, struct { }
var c1 = new C1<System.Enum>(); // legal
var c2 = new C2<System.Enum>(); // compiler error
var c22 = new C2<System.DayOfWeek>(); // legal Even stranger is the combination |
Beta Was this translation helpful? Give feedback.
-
Okay, but it's still crazy to think "OR constraints" is even remotely in the ballpark for the LDM team. |
Beta Was this translation helpful? Give feedback.
-
Still it would be great to have something like I'm not sure how it can be implemented, but lately i found myself needing something like this, for example, I have next code:
And I'm trying to do something like this:
which obviously gives me compilation error It still would be needed to write quite some code but it would be great if it would not be needed to create all specific overloads for CastMyValToInt and stuff. As for implementation, may be some kind of duck typing is a go. |
Beta Was this translation helpful? Give feedback.
-
Real world interop problem which would be solved by modified generic constraints syntax (simplified for clarity): [StructLayout(LayoutKind.Explicit)]
public unsafe struct NativeVector<T> where T : byte, ushort, uint, ulong // compile time error
{
[FieldOffset(0)]
public fixed T [4];
} Another approach which has relaxed syntax - useless in real world code: [StructLayout(LayoutKind.Explicit)]
public unsafe struct NativeVector<T> where T : struct
{
[FieldOffset(0)]
public fixed T [4]; // compile time error
} Currently working solution to the problem is a creation of multitude of NativeVector structures one for every underlying numeric type - altogether 10 (byte, sbyte, short, ushort, int, uint, long, ulong, single, double). Firstly it is a bit of unnecessary work, secondly it pollutes type system with 10 new types which otherwise would be replaced by only one. Now let us imagine what happens if I would need to create additional vectors in 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12 ..... 1024 dimensions. Simple solution which goes outside of the proposal is as follows [StructLayout(LayoutKind.Explicit)]
public unsafe struct NativeVector<T, const int N> where T : byte, sbyte, short, ushort, int, uint, long, ulong, float, double
{
[FieldOffset(0)]
public fixed T [N];
} In case N is limited to 16 instead of creating 160 different NativeVector structures one could use single generic type - saving is more than obvious. This example is the essence of generic programming. |
Beta Was this translation helpful? Give feedback.
-
IIRC there's no mechanism in the CLR to permit for a type of dynamic size. The C# language fakes "fixed" buffers by emitting a nested struct of the const size of the total size of the required memory which is calculated at compile time and set via the The closest thing that C# might be able to get is to emit a different |
Beta Was this translation helpful? Give feedback.
-
This proposal is very useful for implementation patterns from #749 |
Beta Was this translation helpful? Give feedback.
-
@HaloFour IMO there is no need for types with dynamic size. The size is determined based on generic parameters T and N at compile time. There is no need to emit different types instead one would emit only different parametrized instances of one type at runtime. This would be close to solutions used in dynamic type languages like javascript but if that does the job it's even better bcs someone already implemented feature and tested it in production. |
Beta Was this translation helpful? Give feedback.
-
You'd need that dynamic sizing to make the buffer work. Fixed buffers don't exist in the CLR; they are a trick of the compiler, accomplished by expanding the type at compile time. The mechanism used is incompatible with generics because a different nested struct would have to be emitted for every single generic type argument (which can't happen at compile time). Something like a generic size argument would explode that into a Cartesian product. The Javascript type system is effectively the polar opposite from the CLR type system. |
Beta Was this translation helpful? Give feedback.
-
If Union Types from Union and Intersection Types are allowed, then something like:
would be an alternative, instead of
The advantage would be not to rely on generics. |
Beta Was this translation helpful? Give feedback.
-
@sighoya The meaning of that can be quite different. For example, compare: public T M(T x, T y) where T:byte or sbyte or short;
public Union{byte i1; sbyte i2; short i3;} M(Union{byte i1; sbyte i2; short i3;} x, Union{byte i1; sbyte i2; short i3} y); In the first version, |
Beta Was this translation helpful? Give feedback.
-
@svick |
Beta Was this translation helpful? Give feedback.
-
As per my proposed syntax in #399 the constrained generic type code looks like- public T Add<T>(T x, T y) where T : byte | sbyte | short
=> x + y; |
Beta Was this translation helpful? Give feedback.
-
It is a nice thing to treat T as Union of byte sbyte and short by:
We can then call:
but also:
Why?, A way to solve it is to treat typeof(T) to be either Integer or String depending on what we throw in it, even if we specify T to be a Union<String,Int> It is only an implementation detail, but it is important. |
Beta Was this translation helpful? Give feedback.
-
Question: Why is the code in my comment above not syntax colored? |
Beta Was this translation helpful? Give feedback.
-
public void fun(T1 a, T2 b) where T1 : int | string, T2 : int | string Or if you want to simplify- public void fun(int | string a, int | string b) Use "```csharp" to start code block. And close it with triple ticks to get syntax coloring. |
Beta Was this translation helpful? Give feedback.
-
@gulshan Most Implementations require the latter in order to deny: f(1,"hello") when T:Union<String,Int> |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions