generics

How to create a generic method in Dart?

做~自己de王妃 提交于 2020-05-12 19:14:02
问题 I'm trying to use generic methods in Dart (1.22.0-dev.10.3). Here is a simple example: abstract class VR<T> { VR(); bool foo<T>(T value); } class VRInt extends VR<int> { VRInt(); bool foo<int>(int n) => n > 0; // Thinks n is Object } class VRString extends VR<String> { VRString(); bool foo<String>(String s) => s.length > 0; // Thinks s is Object } Both subclasses generate errors that say the argument to foo is an Object. I'm sure this is just a syntactic error on my part, but I've searched

How to create a generic method in Dart?

▼魔方 西西 提交于 2020-05-12 19:10:36
问题 I'm trying to use generic methods in Dart (1.22.0-dev.10.3). Here is a simple example: abstract class VR<T> { VR(); bool foo<T>(T value); } class VRInt extends VR<int> { VRInt(); bool foo<int>(int n) => n > 0; // Thinks n is Object } class VRString extends VR<String> { VRString(); bool foo<String>(String s) => s.length > 0; // Thinks s is Object } Both subclasses generate errors that say the argument to foo is an Object. I'm sure this is just a syntactic error on my part, but I've searched

MyPy doesn't allow constrained TypeVar's to be covariant? Defining a generic dict with constrained but covariant key-val types

隐身守侯 提交于 2020-05-12 04:48:48
问题 I'm trying to define a custom generic dict, whose keys are of type T_key and values are of type T_val . I also want to put constraints on T_key and T_val , such that T_key can only be of type A or B or their subclass. How do I accomplish this? from typing import TypeVar, Generic class A: ... class B: ... class Asub(A): ... class Bsub(B): ... T_key = TypeVar('T_key', A, B, covariant=True) T_val = TypeVar('T_val', A, B, covariant=True) class MyDict(Generic[T_key, T_val]): ... w: MyDict[ A, B] x

MyPy doesn't allow constrained TypeVar's to be covariant? Defining a generic dict with constrained but covariant key-val types

China☆狼群 提交于 2020-05-12 04:47:18
问题 I'm trying to define a custom generic dict, whose keys are of type T_key and values are of type T_val . I also want to put constraints on T_key and T_val , such that T_key can only be of type A or B or their subclass. How do I accomplish this? from typing import TypeVar, Generic class A: ... class B: ... class Asub(A): ... class Bsub(B): ... T_key = TypeVar('T_key', A, B, covariant=True) T_val = TypeVar('T_val', A, B, covariant=True) class MyDict(Generic[T_key, T_val]): ... w: MyDict[ A, B] x

Get sum of elements of an Array in C# using generics

喜你入骨 提交于 2020-05-12 03:02:27
问题 I want to write a method which can take an arbitrary array of a numeric type and return the sum of all the elements between startIndex and endIndex. This is what I have: private static T SumArrayRange<T>(T[] scores, int startIndex, int endIndex) { T score = 0; for (int i = startIndex; i <= endIndex; i++) { score += scores[i]; } return score; } But the compilation fails with these 2 errors. Cannot implicitly convert type 'int' to 'T'. Operator '+=' cannot be applied to operands of type 'T' and

Get sum of elements of an Array in C# using generics

风格不统一 提交于 2020-05-12 03:01:12
问题 I want to write a method which can take an arbitrary array of a numeric type and return the sum of all the elements between startIndex and endIndex. This is what I have: private static T SumArrayRange<T>(T[] scores, int startIndex, int endIndex) { T score = 0; for (int i = startIndex; i <= endIndex; i++) { score += scores[i]; } return score; } But the compilation fails with these 2 errors. Cannot implicitly convert type 'int' to 'T'. Operator '+=' cannot be applied to operands of type 'T' and

Java generic type issue

走远了吗. 提交于 2020-05-11 10:51:39
问题 Consider the following simplified example: package com.test; class B<S> { B(Class<S> clazz) {} } class A<T> { class SubB extends B<SubB> { SubB() { super(SubB.class); } } } Although IntelliJ is not showing any error (as it usually does when compile errors exist), the actual compilation when starting the program ends with error located in super(SubB.class); : Error:(8, 23) java: incompatible types: java.lang.Class<com.test.A.SubB> cannot be converted to java.lang.Class<com.test.A<T>.SubB> I am

How to get class of generic type parameter in Kotlin

99封情书 提交于 2020-05-11 07:40:22
问题 I would like get the class property from a generic type T . I've decided to extend to Any but I'm getting an error. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html#extension-properties I have the following code: class FirebaseDBRepo<T : Any>(val child:String) { private var callback: FirebaseDatabaseRepositoryCallback<T>? = null private val ref: DatabaseReference private val listener = object : ValueEventListener { override fun onDataChange(dataSnapshot: DataSnapshot) { //T

How to get TYPE_USE annotations on a generic bound

£可爱£侵袭症+ 提交于 2020-05-10 18:48:10
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

How to get TYPE_USE annotations on a generic bound

末鹿安然 提交于 2020-05-10 18:43:08
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod