generics

Method signature selection for lambda expression with multiple matching target types

泄露秘密 提交于 2020-06-10 07:51:11
问题 I was answering a question and ran into a scenario I can't explain. Consider this code: interface ConsumerOne<T> { void accept(T a); } interface CustomIterable<T> extends Iterable<T> { void forEach(ConsumerOne<? super T> c); //overload } class A { private static CustomIterable<A> iterable; private static List<A> aList; public static void main(String[] args) { iterable.forEach(a -> aList.add(a)); //ambiguous iterable.forEach(aList::add); //ambiguous iterable.forEach((A a) -> aList.add(a)); /

Creating expression tree for accessing a Generic type's property

岁酱吖の 提交于 2020-06-10 02:53:29
问题 I need to write a generic method which takes the instance of the generic type and the property name in string format and return an Expression tree I need to convert a simple lambda expression a => a.SomePropertyName where a is generic type which will have a property by the name SomePropertyName I know that we can get the property information using the following reflection code System.Reflection.PropertyInfo propInfo = a.GetType().GetProperty("SomePropertyName"); This might be very simple, but

<? extends Class> and <? super Class> in Java - why it works this way?

情到浓时终转凉″ 提交于 2020-06-09 07:56:29
问题 Yet another novice, trying to comprehend Java Generics. I've observed all topics, I found, but I still have huge questions. Could you please explain me the following things: <? extends SomeClass> means, that ? is "any type", and extends SomeClass means, that this any type can be only a subclass of SomeClass . OK, I write two elementary classes: abstract class Person { private String name; public Person(String name) { this.name = name; } } class Student extends Person { public Student(String

What does var<T> do in Java?

不羁岁月 提交于 2020-06-08 16:56:51
问题 A friend of mine noticed that var<Integer> list = new ArrayList<Double>(); was valid in Java. It turns out that the type of list is evaluated to ArrayList<Double> . When using var<Integer> list = new ArrayList<>(); , list is just ArrayList<Object> . Both of us were not able to figure out, what the generic type of var does, as it seems to be ignored. But if so, why is this even syntactically correct in the first place? 回答1: This is indeed a bug, but the proof lies in the Java Language

Can you declare a object literal type that allows unknown properties in typescript?

偶尔善良 提交于 2020-06-08 16:15:13
问题 Essentially I want to ensure that an object argument contains all of the required properties, but can contain any other properties it wants. For example: function foo(bar: { baz: number }) : number { return bar.baz; } foo({ baz: 1, other: 2 }); But this results in: Object literal may only specify known properties, and 'other' does not exist in type '{ baz: number; }'. 回答1: Yes, you can. Try this: interface IBaz { baz: number; [key: string]: any; } function foo(bar: IBaz) : number { return bar

What does the `*` mean in a generic type?

那年仲夏 提交于 2020-06-07 07:06:48
问题 I was learning Cats library and found * as a generic type, like that: implicit def catsDataSemigroupKForValidated[A](implicit A: Semigroup[A]): SemigroupK[Validated[A, *]] = new SemigroupK[Validated[A, *]] { def combineK[B](x: Validated[A, B], y: Validated[A, B]): Validated[A, B] = x match { case v @ Valid(_) => v case Invalid(ix) => y match { case Invalid(iy) => Invalid(A.combine(ix, iy)) case v @ Valid(_) => v } } } My guess is that the * is used, because the combineK method return

Recursive generic function with type passed as a parameter

旧巷老猫 提交于 2020-06-01 05:22:07
问题 I want to have a recursive generic function, but I cannot use type passed as an argument in generic function invocation, cause of 'memberType' refers to a value, but is being used as a type here. Is there a way to pass memberType to generic method invocation? Example: class Packet { header: Header body: Body static MEMBERS = [ ['header', Header, 0, 6], ['body', Body, 6, 10], ] } class Header { size: number ttl: number static MEMBERS = [ ['size', 'number', 0, 2], ['ttl', 'number', 2, 3], ] }

how to convert generic rdd to dataframe?

不羁的心 提交于 2020-05-31 03:56:06
问题 I am writing a method that takes an rdd and saves it as an avro file. The problem is that if I use a specific type than I can do .toDF() but I cannot call .toDF() on a generic rdd! Here is an example: case class Person(name: String) def f(x: RDD[Person]) = x.toDF() def g[T](x: RDD[T]) = x.toDF() f(p) //works g(p) //fails!! Does anyone know why I can't call .toDF() on a generic rdd and if there is any way around it? 回答1: If you are using Spark 2, import org.apache.spark.sql.Encoder def g[T:

Creating a parser of Class name + String value to a typed value

谁都会走 提交于 2020-05-30 08:12:11
问题 I am trying to write a method that can take in a String classname and a String value, and return the value represented as that String. Example inputs: parse("java.lang.String", "abc") -> String "ABC" parse("java.lang.Boolean", "FALSE") -> Boolean FALSE parse("java.lang.Integer", "123") -> Integer 123 parse("com.me.Color", "RED") -> enum Color.RED I have found that if I use an if block containing assignableFrom calls, I can achieve this. But would prefer writing something more extendable, so

In Haxe, can you write a generic interface where a method type parameter is constrained by the class's type parameter?

♀尐吖头ヾ 提交于 2020-05-30 03:54:29
问题 I'm having trouble writing the generic interface below. In my class, I have a function that takes an array of < any type that extends a parent class > and traces its first element. Since I'm only reading elements from the array, I'm using it as if its a covariant compound type, and therefore I'm guaranteed to have the cast statement never fail. Now I want to abstract this out even more and write a interface that defines fn using another generic type T. I want fn to be able to accept any Array