generics

How can I call a generic function with just one generic parameter when the second one is needed on the fly?

∥☆過路亽.° 提交于 2020-07-23 06:37:44
问题 I have a generic function with two parameters. The second one is based on the value of first parameter. The code works with just one type variable but when I add another one, cannot call the function. This works perfectly: type Event<T> = T extends 'string' ? string : number; type input = 'string' | 'number'; function test<U extends input>(module: U, event: Event<U>) { //... } //No errors test('number', 2); // Generates error as expected test('string', 2); But when another generic variable is

How to do if-else for some specific generic type?

我只是一个虾纸丫 提交于 2020-07-23 06:18:09
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;

How to do if-else for some specific generic type?

余生长醉 提交于 2020-07-23 06:17:41
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;

How to do if-else for some specific generic type?

孤者浪人 提交于 2020-07-23 06:16:25
问题 I have MyClass and I want to call both mySubMethod(key) and mySubMethod2(key) for integer, but only call mySubMethod2(key) for non-integer. I tried below codes but doesn't work. I also tried to change K == int to something else, e.g. K == Integer , K.equals(int) and K.equals(Integer) etc., but all of them don't work. How Can I fix it? Thanks. public class MyClass<K,V> { public boolean myMethod(K key) { if (K == int) { mySubMethod(key); }else { // do nothing } mySubMethod2(key); return false;

How can I use vararg with different generics in Kotlin?

眉间皱痕 提交于 2020-07-22 10:08:26
问题 I want to use vararg with generics with different types of each arguments what I've already tried: class GeneralSpecification<T> { fun <P> ifNotNullCreateSpec(vararg propToCreateFun: Pair<P?, (P) -> Specification<T>>): List<Specification<T>> = propToCreateFun.mapNotNull { (prop, funCreateSpec) -> prop?.let(funCreateSpec) } ... } but I can't use this like: ifNotNullCreateSpec("asdf" to ::createStringSpec, 5 to ::createIntSpec) (different types in vararg pairs) How can I use vararg with

How can I use vararg with different generics in Kotlin?

不羁岁月 提交于 2020-07-22 10:08:04
问题 I want to use vararg with generics with different types of each arguments what I've already tried: class GeneralSpecification<T> { fun <P> ifNotNullCreateSpec(vararg propToCreateFun: Pair<P?, (P) -> Specification<T>>): List<Specification<T>> = propToCreateFun.mapNotNull { (prop, funCreateSpec) -> prop?.let(funCreateSpec) } ... } but I can't use this like: ifNotNullCreateSpec("asdf" to ::createStringSpec, 5 to ::createIntSpec) (different types in vararg pairs) How can I use vararg with

C# No implicit conversion to inherited type

耗尽温柔 提交于 2020-07-22 05:51:09
问题 I'm hoping someone can explain this to me as I'm confused about it. I have a series of inheritance amongst my classes and interfaces, with a factory to instantiated the correct object. However, I am getting a Cannot implicitly convert type 'Conn' to IConn. An explicit conversion exists (are you missing a cast?) Here is an trimmed down version of the code. public abstract class Id { public int Id { get; set; } } public abstract class Id_SourceId { public int SourceId { get; set; } } public

Cast generic type parameter to a specific type in C#

回眸只為那壹抹淺笑 提交于 2020-07-18 03:37:18
问题 If you need to cast a generic type parameter to a specific type we can cast it to a object and do the casting like below, void SomeMethod(T t) { SomeClass obj2 = (SomeClass)(object)t; } Is there a better way to achieve this rather than casting it to a object and then to a specific type. Problem I have a generic function which accepts a generic type parameter, inside the function based on a type checking I do some operations like below void SomeMethod(T t) { if (typeof(T).Equals(typeof(TypeA))

Static Method in Interface with Generic signature

笑着哭i 提交于 2020-07-17 10:20:16
问题 As of Java 8 you can have default or static methods implemented in Interfaces as the below public interface DbValuesEnumIface<ID, T extends Enum<T>> { T fromId(ID id); ID getId(); static String getDescriptionKey(){ return "this is a test"; } } I would like to declare the above with the static method having a signature that uses bounds defined by the implementing classes since the method's implementation should be the same for all,with the only thing different should be the generics declared,

Base class constraint on generic class specifying the class itself

给你一囗甜甜゛ 提交于 2020-07-15 11:13:04
问题 Yesterday, I was explaining C#'s generic constraints to my friends. When demonstrating the where T : CLASSNAME constraint, I whipped up something like this: public class UnusableClass<T> where T : UnusableClass<T> { public static int method(T input){ return 0; } } And was really surprised to see it compile. After a bit of thinking, however, I figured it was perfectly legal from the point of view of the compiler - UnusableClass<T> is as much of a class as any other that can be used in this