generic-constraints

Instantiating a type-constrained generic class without subclassing it

痴心易碎 提交于 2019-12-14 03:16:31
问题 Background [Skip to Question if you're not interested in the background] I stumbled across this generic class definition when reading the other day, and it stumped me for some time: public abstract class Entity<T> where T : Entity<T> I was puzzled as to how T on Entity<T> could be of Type Entity<T> itself. It seemed some sort of bizarre recursive constraint. I then realised that this constraint could be satisfied by subclassing (which is, of course, what abstract is demanding of the class):

Why can't we satisfy F# static member constraints with type extensions?

我只是一个虾纸丫 提交于 2019-12-12 11:10:46
问题 I'd like to be able to extend types from other libraries with static methods to enable generic arithmetic. Take, for example, the newly minted SIMD-friendly fixed-size VectorN types from Microsoft. They define Zero , they define (+) , they define (/) , but I can't use Array.average on them because they don't define DivideByInt , which I'd be happy to: open System.Numerics type Vector2f with static member DivideByInt (v:Vector2f) (i:int) = v / Vector2f(single i, single i) let bigArray :

Possible Bug? I can create generic instance ignoring constraint

廉价感情. 提交于 2019-12-07 20:09:56
问题 While working on a node based framework including serialization and deserialization i stummbeld over a case where i can create an instance of a class ignoring the type constraint. Why can i create a ZNumericProcessor<String> inside ZSum although ZNumericProcessor is definded as ZNumericProcessor<T: ZNumeric> where String does not conform to ZNumeric ? And why does it even works that the initializers are called although for ZSum it is not sure that T is ZNumeric ? Normally i woud have thought

F#: Member constraints to help create seemingly dynamic types

泄露秘密 提交于 2019-12-07 14:26:36
问题 I've been looking into a way to add some duck typing to an F# method. SomeMethod(model:'a) = let someField = model.Test("") Where the parameter coming in has the Test method on it. I've seen notation like this: member inline public x.Testing< ^a when ^a : (member public Test : String-> String)>(model:^a) = let something = model.Test("") ignore Which looks like to me that generic constraints can be used to enfore at a method level rather than class/interface level. Problem is I can't get it to

Possible Bug? I can create generic instance ignoring constraint

孤街浪徒 提交于 2019-12-06 09:49:32
While working on a node based framework including serialization and deserialization i stummbeld over a case where i can create an instance of a class ignoring the type constraint. Why can i create a ZNumericProcessor<String> inside ZSum although ZNumericProcessor is definded as ZNumericProcessor<T: ZNumeric> where String does not conform to ZNumeric ? And why does it even works that the initializers are called although for ZSum it is not sure that T is ZNumeric ? Normally i woud have thought that i can`t even have the constraint on the init of ZSum to get it to compile. Second: If i would not

Generic constraint: Enforce type to have static function and constructor with parameters

本小妞迷上赌 提交于 2019-12-06 05:29:04
问题 I know you can write: class GenericClass<T> where T : new() { } to enforce that T has an empty constructor. My Qs are : can you enforce that T has a constructor with a specific type of parameter? Like: class SingletonFactoryWithEmptyConstructor<T> where T : new(int) can you enforce that T has a static function (let's say, void F() ) so that you can use this function inside the generic class? Like : class GenericClass<T> where T : void F() { void G () { T.F(); } } I know you can specify that T

Why can't System.Array be a type constraint?

房东的猫 提交于 2019-12-04 03:08:31
问题 I'm working on a small project with a few different types of arrays (e.g. double[] , float[] , int[] . For verification / testing / sanity purposes, I'm printing out some of these arrays to the console as I go along. So I have multiple functions that look like these below (simplified for this example - assume I'm only dealing with single-dimension arrays): void Print(float[] a) // prints an array of floats { for (int i = 0; i < a.Length; i++) { Console.Write(a[i]); } } void Print(double[] a)

TypeScript - can a generic constraint provide “allowed” types?

送分小仙女□ 提交于 2019-12-04 00:46:33
问题 Given the following code... type Indexable<TKey, TValue> = { [index: TKey]: TValue } This produces the following error: An index signature parameter type must be 'string' or 'number'. Is there a way to constrain TKey to be 'string' or 'number'? 回答1: As @TitianCernicova-Dragomir indicates, you can't use TKey as the type in an index signature, even if it is equivalent to string or number. If you know that TKey is exactly string or number , you can just use it directly and not specify TKey in

Why does Nullable<T> not match as a reference type for generic constraints [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 00:22:56
Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass<T> where T:class { } However, I've found I can't use nullable types as I'd expect: new SomeClass<int?>(); I get an error that int? must be a reference type. Is Nullable really just a struct with syntactic sugar to make it look like a reference type? Nullable<T> is a struct (see MSDN ) however it is the only struct that does not satisfy the struct constraint. Therefore, you cannot use a Nullable as a generic type parameter

Member with the same signature already defined with different type constraints

烈酒焚心 提交于 2019-12-01 04:25:31
I have run across an issue with overloading methods that have different constraints that seems exclusive. That is my example: public class A { public void Do<T>() where T : class { } public void Do<T>() where T : struct { } } And this does not compile with the following error "Member with the same signature already defined". Is it possible to satisfy both conditions at once or it's just the limitation of the C# compiler? Jon Skeet It's not a limitation of the compiler - it's a limitation of the language (and quite possibly the CLR as well; I'm not sure). Fundamentally those are clashing