generics

Kotlin - Generic Type Parameters Not Being Respected

不问归期 提交于 2020-01-22 14:33:22
问题 Consider the following example: import kotlin.reflect.KProperty1 infix fun <T, R> KProperty1<T, R>.test(value: R) = Unit data class Foo(val bar: Int) fun main() { Foo::bar test "Hello" } Given that test expects a value of type R , why in this context, where the property type is Int , does it allow me to pass a String ? 回答1: First, take a look at the declaration of the interface KProperty1 , which is: interface KProperty1<T, out R> : KProperty<R>, (T) -> R The important part here is the out

Should I be using a Generic Repository with Entity Framework 5?

不打扰是莪最后的温柔 提交于 2020-01-22 13:37:12
问题 I'm currently using Entity Framework with a Generic Repository and Unit Of Work Pattern. My Model is similar to the one described in this article I've used Generic Repositories in the past and really enjoyed the global functionality it can provide. However, it seems I'm running into more problems every single day when it comes to using it with Entity Framework. These problems seem to arise even more when it comes to handling Parent/Children/Junction relationships. Using a Generic Repository

Should I be using a Generic Repository with Entity Framework 5?

孤街浪徒 提交于 2020-01-22 13:36:05
问题 I'm currently using Entity Framework with a Generic Repository and Unit Of Work Pattern. My Model is similar to the one described in this article I've used Generic Repositories in the past and really enjoyed the global functionality it can provide. However, it seems I'm running into more problems every single day when it comes to using it with Entity Framework. These problems seem to arise even more when it comes to handling Parent/Children/Junction relationships. Using a Generic Repository

Java:How to override this generic method?

[亡魂溺海] 提交于 2020-01-22 11:29:45
问题 public <S extends T> List<S> save(Iterable<S> entities) { //... } If I use following method to override @Override public List<MyType> save(Iterable<MyType> structures) { List<MyType> result = new ArrayList<>(); //... return result; } I get following error: method does not override or implement a method from a supertype name clash: save(Iterable<MyType>) in MyTypeRepositoryImpl and <S>save(Iterable<S>) in SimpleJpaRepository have the same erasure, yet neither overrides the other where S,T are

What's this generics usage in Java? X.<Y>method()

蓝咒 提交于 2020-01-22 10:37:08
问题 I've read the whole SCJP6 book Sierra and Bates book, scored 88% the exam. But still, i never heard of how this kind of code works as it's not explained in the generics chapter: Collections.<TimeUnit>reverseOrder() What is this kind of generics usage? I discovered it in some code but never read anything about it. It seems to me it permits to give some help to type inference. I've tried to search about that but it's not so easy to find (and it's not even in the SCJP book/exam!) So can someone

How to write strongly typed generic extension function in Kotlin?

淺唱寂寞╮ 提交于 2020-01-22 10:15:09
问题 Focus on the strong and generic parts. Let's say I have this extension function: fun <E> Collection<E>.myContains(item: E) : Boolean { // quite pointless, I know, but a simple example return item in this } the intention is to write a function that only accepts types of the collection elements ( E ), but this is not validated by the compiler?! val isItInside: Boolean = listOf(1, 2).myContains("1") happily compiles. My guess is that E is inferred to be Any . How can I enforce this restriction

Java Generics: Comparing the class of Object o to <E>

老子叫甜甜 提交于 2020-01-22 08:29:23
问题 Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object o) { // TODO help! } } How would I check that o is the same class as E ? Test<String> test = new Test<String>(); test.sameClassAs("a string"); // returns true; test.sameClassAs(4); // returns false; I can't change the method signature from (Object o) as I'm overridding a superclass and so don't get to choose my method signature. I would also rather not go down the road of attempting a cast and then

Java Generics: Comparing the class of Object o to <E>

我的未来我决定 提交于 2020-01-22 08:28:12
问题 Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object o) { // TODO help! } } How would I check that o is the same class as E ? Test<String> test = new Test<String>(); test.sameClassAs("a string"); // returns true; test.sameClassAs(4); // returns false; I can't change the method signature from (Object o) as I'm overridding a superclass and so don't get to choose my method signature. I would also rather not go down the road of attempting a cast and then

Java Generics casts strangely [duplicate]

孤街醉人 提交于 2020-01-22 08:01:10
问题 This question already has answers here : Why does this use of Generics not throw a runtime or compile time exception? (3 answers) Closed 2 years ago . I am using java 8. I recently came across this: public class Test { public static void main(String[] args) { String ss = "" + (Test.<Integer>abc(2)); System.out.println(Test.<Integer>abc(2)); } public static <T> T abc(T a) { String s = "adsa"; return (T) s; } } This does not throw a java.lang.ClassCastException. Why is that? I always thought +

reference to generic type in XML code comment [duplicate]

北城余情 提交于 2020-01-22 04:57:07
问题 This question already has answers here : How to reference generic classes and methods in xml documentation (7 answers) Closed 6 years ago . As I know, in a XML comment for a C# type/method, it is possible to reference a generic type in a tag like so: ///<see cref="name.space.typename<T&rt;(paramtype)"> But I think, there was another syntax, which is less clumsy? Something, to get rid of those html entities '<'? I cannot find it right now. Can somebody help? 回答1: Here's a good article on