generic-constraints

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

依然范特西╮ 提交于 2019-12-01 03:36:54
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'? 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 your type: type StringIndexable<TValue> = { [index: string]: TValue } type NumberIndexable<TValue> = { [index

How to add LanguagePrimitives.GenericZero / get_Zero to System.String?

ぐ巨炮叔叔 提交于 2019-12-01 00:51:43
问题 Note: I added a lot of Of interest comments at the end. These are not mean to suggest that one should use inline and static type parameters willy nilly, they are there so that one does not have to spend hours searching lots of SO questions related to this question to better understand these concepts. I know that when one needs to make a function generic and needs a zero (0) value F# provides GenericZero. Resolves to the zero value for any primitive numeric type or any type with a static

What exactly is a “Special Class”?

强颜欢笑 提交于 2019-11-29 20:21:25
After failing to get something like the following to compile: public class Gen<T> where T : System.Array { } with the error A constraint cannot be special class `System.Array' I started wondering, what exactly is a "special class"? People often seem to get the same kind of error when they specify System.Enum in a generic constraint. I got the same results with System.Object , System.Delegate , System.MulticastDelegate and System.ValueType too. Are there more of them? I cannot find any info on "special classes" in C#. Also, what is so special about those classes that we can't use them as a

What exactly is a “Special Class”?

天涯浪子 提交于 2019-11-28 16:34:03
问题 After failing to get something like the following to compile: public class Gen<T> where T : System.Array { } with the error A constraint cannot be special class `System.Array' I started wondering, what exactly is a "special class"? People often seem to get the same kind of error when they specify System.Enum in a generic constraint. I got the same results with System.Object , System.Delegate , System.MulticastDelegate and System.ValueType too. Are there more of them? I cannot find any info on

Why aren't generic type constraints inheritable/hierarchically enforced

被刻印的时光 ゝ 提交于 2019-11-27 04:03:53
Item class public class Item { public bool Check(int value) { ... } } Base abstract class with generic type constraint public abstract class ClassBase<TItem> where TItem : Item { protected IList<TItem> items; public ClassBase(IEnumerable<TItem> items) { this.items = items.ToList(); } public abstract bool CheckAll(int value); } Inherited class without constraints public class MyClass<TItem> : ClassBase<TItem> { public override bool CheckAll(int value) { bool result = true; foreach(TItem item in this.items) { if (!item.Check(value)) // this doesn't work { result = false; break; } } return result

Is there a generic constructor with parameter constraint in C#?

偶尔善良 提交于 2019-11-26 14:30:48
In C# you can put a constraint on a generic method like: public class A { public static void Method<T> (T a) where T : new() { //...do something... } } Where you specify that T should have a constructor that requires no parameters. I'm wondering whether there is a way to add a constraint like " there exists a constructor with a float[,] parameter? " The following code doesn't compile: public class A { public static void Method<T> (T a) where T : new(float[,] u) { //...do something... } } A workaround is also useful? Tim Robinson As you've found, you can't do this. As a workaround I normally

Why aren&#39;t generic type constraints inheritable/hierarchically enforced

旧巷老猫 提交于 2019-11-26 11:01:57
问题 Item class public class Item { public bool Check(int value) { ... } } Base abstract class with generic type constraint public abstract class ClassBase<TItem> where TItem : Item { protected IList<TItem> items; public ClassBase(IEnumerable<TItem> items) { this.items = items.ToList(); } public abstract bool CheckAll(int value); } Inherited class without constraints public class MyClass<TItem> : ClassBase<TItem> { public override bool CheckAll(int value) { bool result = true; foreach(TItem item

Is there a generic constructor with parameter constraint in C#?

…衆ロ難τιáo~ 提交于 2019-11-26 03:55:43
问题 In C# you can put a constraint on a generic method like: public class A { public static void Method<T> (T a) where T : new() { //...do something... } } Where you specify that T should have a constructor that requires no parameters. I\'m wondering whether there is a way to add a constraint like \" there exists a constructor with a float[,] parameter? \" The following code doesn\'t compile: public class A { public static void Method<T> (T a) where T : new(float[,] u) { //...do something... } }

Create Generic method constraining T to an Enum

自闭症网瘾萝莉.ら 提交于 2019-11-25 21:48:14
问题 I\'m building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item; } return defaultValue; } I am getting a Error