generics

How to do generic polymorphism on open types in C#?

 ̄綄美尐妖づ 提交于 2021-01-31 05:39:55
问题 I have an interface like: interface IFoo<in T> { ... } And I want to have a collection of those classes WITHOUT forcing a specific generic parameter for all collection elements, like: class IFooCollection { List<IFoo<?>> _items; public IFooCollection(List<IFoo<?>> items) { _items = items; } ... } 回答1: Since you don't want to force a specific type, make an abstract class or an interface this way: interface IFoo { } And inherit it in your type-specific Foo (with generic parameters), like:

How to do generic polymorphism on open types in C#?

。_饼干妹妹 提交于 2021-01-31 05:38:22
问题 I have an interface like: interface IFoo<in T> { ... } And I want to have a collection of those classes WITHOUT forcing a specific generic parameter for all collection elements, like: class IFooCollection { List<IFoo<?>> _items; public IFooCollection(List<IFoo<?>> items) { _items = items; } ... } 回答1: Since you don't want to force a specific type, make an abstract class or an interface this way: interface IFoo { } And inherit it in your type-specific Foo (with generic parameters), like:

Understanding type arguments do not conform to class type parameter bounds error when using Higher kinded type parameter

你。 提交于 2021-01-29 21:55:54
问题 I am trying to understand why the following piece of code won't compile when I use a higher kinded type parameter for T in MyModel abstract class Model[M <: Model[M]] class MyModel[T] extends Model[MyModel[T]] class Bar[TModel <: Model[TModel]] object Foo extends App { new Bar[MyModel[_]] } But if i change it to new Bar[MyModel[Any]] it will compile. Why is this ? 回答1: Bar[MyModel[_]] is Bar[MyModel[X] forSome {type X}] . (It should not be confused neither with Bar[MyModel[X]] forSome {type X

Can I define a Typescript map having a value constraint corresponding with each value's key?

孤人 提交于 2021-01-29 17:21:34
问题 In this playground I would like to create a map containing at most a single action of each type in the Action union. Each of the unioned types is differentiated by having a different string literal property 'type'. I have a definition for this map which compiles but is too loose... const lastAction:{ [A in Action["type"]]?:Action } = {} The constraint on keys within the lastAction map enforces that... the key is a "type" property from some Action type the value must be some Action type ...it

Constraining type parameters on case classes and traits

本秂侑毒 提交于 2021-01-29 14:27:07
问题 I'm a bit confused about how to constrain type parameters so that they will only accept types that have implemented a particular typeclass. Here's some rather contrived sample code: // I want to tag my favourite types of number with this trait trait MyFavouriteTypesOfNumber[A] // implicits necessary because I cannot directly extend Int or Double implicit def iLikeInts(i: Int): MyFavouriteTypesOfNumber[Int] = new MyFavouriteTypesOfNumber[Int] {} implicit def iAlsoLikeFloats(f: Float):

Swift: casting un-constrained generic type to generic type that confirms to Decodable

淺唱寂寞╮ 提交于 2021-01-29 11:08:10
问题 Situation I have a two generic classes which will fetch data either from api and database, lets say APIDataSource<I, O> and DBDataSource<I, O> respectively I will inject any of two class in view-model when creating it and view-model will use that class to fetch data it needed. I want view-model to work exactly same with both class. So I don't want different generic constraints for the classes // sudo code ViewModel(APIDataSource <InputModel, ResponseModel>(...)) // I want to change the

Register Open Generic with dotnet core 3

送分小仙女□ 提交于 2021-01-29 09:21:45
问题 I'm trying to register an Open Generic type in my application but I'm having a strange behavior. The following is working correctly: services.AddTransient(typeof(IA<>), typeof(A<>)); Now, I would like to pass parameters to my A class, because my constructor has some arguments: services.AddTransient(typeof(IA<>), provider => { return ActivatorUtilities.CreateInstance(provider, typeof(A<>), "1", "2"); }); This generates an exception 'Open generic service type 'IA`1[T]' requires registering an

Cannot convert Type `ClassA` to T

匆匆过客 提交于 2021-01-29 09:08:45
问题 I'm writeing a generice method in C#: private T GetMamConfigurations<T>(IDictionary<string, object> items, MaMDBEntities maMDBEntities) where T : class { T geoConfigs = default(T); if (typeStr.Equals("MamConfiguration", StringComparison.OrdinalIgnoreCase)) { geoConfigs = (T)GetGeoConfigurationNumericFromDB(items, maMDBEntities); } else if (typeStr.Equals("ListOfMamConfiguration", StringComparison.OrdinalIgnoreCase)) { geoConfigs = (T)GetGeoConfigurationsPercentageFromDB(items, maMDBEntities);

Type erasure problem in method overloading

走远了吗. 提交于 2021-01-29 09:04:27
问题 I have two overloaded method having following signatures - def fun(x: Seq[String]): Future[Seq[Int]] = ??? def fun(x: Seq[(String, String)]): Future[Seq[Int]] = ??? Due to type erasure, these methods can't be overloaded and hence showing compilation error. I tried using typetags as a workaround - def fun[t: TypeTag](values: Seq[T]): Future[Seq[Int]] = { typeOf[T] match { case t if t =:= typeOf[String] => ??? case t if t =:= typeOf[(String, String)] => ??? case _ => ??? // Should not come here

Restrict generic key type based on lookup type in TypeScript

浪尽此生 提交于 2021-01-29 06:15:50
问题 I'm working on a function that takes two type parameters, T and K . T extends a Record type and K is a key of the first type. Is there a way I can restrict the key type based on its lookup type ( T[K] ) in T ? I have the following types: type FormValue = string | number | boolean | null; type FormValues = Record<string, FormValue>; and the following function: function numericFormHelperFunc<T extends FormValues, K extends keyof T>(key: K, formValues: T) {} Is there a way I can restrict which