parameterized-types

Understanding bounded generics in java. What is the point?

瘦欲@ 提交于 2020-02-13 05:27:16
问题 I am trying to understand bounded types and not quite grasping the point of them. There is an example of bounded generics on which provides this use case: public class NaturalNumber<T extends Integer> { private T n; public NaturalNumber(T n) { this.n = n; } public boolean isEven() { return n.intValue() % 2 == 0; } // ... } If you are going to restrict the classes that can be the parameterized type, why not just forget the parameterization all together and have: public class NaturalNumber {

Understanding bounded generics in java. What is the point?

混江龙づ霸主 提交于 2020-02-13 05:27:14
问题 I am trying to understand bounded types and not quite grasping the point of them. There is an example of bounded generics on which provides this use case: public class NaturalNumber<T extends Integer> { private T n; public NaturalNumber(T n) { this.n = n; } public boolean isEven() { return n.intValue() % 2 == 0; } // ... } If you are going to restrict the classes that can be the parameterized type, why not just forget the parameterization all together and have: public class NaturalNumber {

Java object casting doesn't work like I need it to

南楼画角 提交于 2019-12-12 04:46:20
问题 I'm developing native Android app on Java and I faced some unexpected behavior. Look at these classes: public class Parent<C> { protected C mCallback; public void setCallback(Object callback) { mCallback = (C)callback; } } class Child<T extends Vehicle> extends Parent<Child.Callback<Vehicle>>{ public void invoke(){ mCallback.onAction(new Vehicle()); } public interface Callback<T> { void onAction(T vehicle); } } class Vehicle { } And now Child<Vehicle> child = new Child<Vehicle>(); child

scala parameterized type with constraint appearing after the colon?

纵然是瞬间 提交于 2019-12-12 03:38:42
问题 I was looking at the source for Sorting.scala, and was curious about the definition of the last method in the source snippet below. object Sorting { /** Quickly sort an array of Doubles. */ def quickSort(a: Array[Double]) { sort1(a, 0, a.length) } /** Quickly sort an array of items with an implicit Ordering. */ def quickSort[K: Ordering](a: Array[K]) { sort1(a, 0, a.length) } //<<?? The type parameter 'K' seems to be constrained to be a subtype (perhaps?) of 'Ordering'... But I have never

Java selectionSort Generic Class

天涯浪子 提交于 2019-12-12 01:06:28
问题 I am trying to program an array sorter, so far this is what i have. When i run it i get array out of bounds exception. I dont know where the array is out of bounds. can someone help me figure out where? public static void main(String[] args) { Integer[] list50k = new Integer[50000]; // Create random array of 50k for (int i = 0; i < 50000; i++){ Integer x = random.nextInt(9) + 1; list50k[i] = x; } selectionSort(list50k); public static <T extends Comparable<T>> void selectionSort(T[] list){ for

Can Apache Avro framework handle parameterized types during serialization?

醉酒当歌 提交于 2019-12-08 03:09:45
问题 Can Apache Avro handle parameterized types during serialization? I see this exception thrown from Avro framework when I try to serialize an instance that uses generics - org.apache.avro.AvroTypeException: Unknown type: T at org.apache.avro.specific.SpecificData.createSchema(SpecificData.java:255) at org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:514) at org.apache.avro.reflect.ReflectData.createFieldSchema(ReflectData.java:593) at org.apache.avro.reflect.ReflectData

flow generic type for function expression (arrow functions)

左心房为你撑大大i 提交于 2019-12-03 17:31:02
问题 I usually try to keep flow function types separate from their implementation. It's a slightly more readable when I write: type Fn = string => string; const aFn: Fn = name => `hello, ${ name }`; rather than: const aFn = (name: string): string => `hello, ${ name }`; When using generic types we can write: const j= <T>(i: T): T => i; const jString: string = j('apple'); // √ const jNumber: number = j(7); // √ But how can I separate this type from a function expression? type H<T> = (input: T) => T;

Why is anonymous class required in “super type token” pattern in java

天涯浪子 提交于 2019-12-03 17:09:07
问题 In Neal Gafter's "super type token" pattern (http://gafter.blogspot.com/2006/12/super-type-tokens.html), an anonymous object was used to pass in the parameterized type : class ReferenceType<T>{} /* anonymous subclass of "ReferenceType" */ ReferenceType<List<Integer>> referenceType = new ReferenceType<List<Integer>>(){ }; Type superClass = b.getClass().getGenericSuperclass(); System.out.println("super type : " + superClass); Type genericType = ((ParameterizedType)superClass)

Why is anonymous class required in “super type token” pattern in java

江枫思渺然 提交于 2019-12-03 06:50:46
In Neal Gafter's "super type token" pattern ( http://gafter.blogspot.com/2006/12/super-type-tokens.html ), an anonymous object was used to pass in the parameterized type : class ReferenceType<T>{} /* anonymous subclass of "ReferenceType" */ ReferenceType<List<Integer>> referenceType = new ReferenceType<List<Integer>>(){ }; Type superClass = b.getClass().getGenericSuperclass(); System.out.println("super type : " + superClass); Type genericType = ((ParameterizedType)superClass).getActualTypeArguments()[0]; System.out.println("actual parameterized type : " + genericType); Then result is : super

flow generic type for function expression (arrow functions)

放肆的年华 提交于 2019-12-03 06:32:42
I usually try to keep flow function types separate from their implementation. It's a slightly more readable when I write: type Fn = string => string; const aFn: Fn = name => `hello, ${ name }`; rather than: const aFn = (name: string): string => `hello, ${ name }`; When using generic types we can write: const j= <T>(i: T): T => i; const jString: string = j('apple'); // √ const jNumber: number = j(7); // √ But how can I separate this type from a function expression? type H<T> = (input: T) => T; const h:H<*> = i => i; // --> WHAT SHOULD GO FOR '*'? const hString: string = h('apple'); // X error