generics

Java infering wrong type of a typed HashSet [duplicate]

谁都会走 提交于 2020-01-24 05:23:05
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: ClassCastException when calling TreeSet<Long>.contains( Long.valueOf( someLongValue ) ) Please see the screenshot for the problem: It seems an entry in the Typed Set cylinderIds is suddenly of type String - but how did that happen? The bean is used from within a JSF page, but I always thought that the type system in Java should prevent this ... Any idea what is going wrong here? Using 1.7.0_06 64 Bit on Windows

Spring inject list of generic interface implementations in kotlin

☆樱花仙子☆ 提交于 2020-01-24 04:46:09
问题 Disclaimer: New to Kotlin, may be a easy to solve issue or misunderstood basics. I am trying to inject a List of a specific interface's Spring implementations, in a regular java class this have been easy like this: @Autowired List<IMyClass> myClassList; But in Kotlin doing the following gives me a error @Autowired lateinit private var myClassList: List<IMyClass<Any>> // No beans of '? extends IMyClass<Object>' or 'List<? extends IMyClass<Object>>' types found Doing it like this: @Autowired

Type constraints in Attributes

*爱你&永不变心* 提交于 2020-01-24 03:20:08
问题 I want to write my enum with custom attributes, for example: public enum SomeEnum: long { [SomeAttribute<MyClass1>] Sms = 1, [SomeAttribute<MyClass2>] Email = 2 } but attributes doesn't support generics. Well, the most similar solution is: public enum SomeEnum: long { [SomeAttribute(typeof(MyClass1))] Sms = 1, [SomeAttribute(typeof(MyClass2))] Email = 2 } And here is problem: I want Class1 to be inherited from ICustomInterface , so with generics I can write constraint: [AttributeUsage

Conflicting compile time behaviour using as keyword against generic types in C#

微笑、不失礼 提交于 2020-01-24 02:03:05
问题 When attempting to use the C# "as" keyword against a non-generic type that cannot be cast to, the compiler gives an error that the type cannot be converted. However when using the "as" keyword against a generic type the compiler gives no error: public class Foo { } public class Bar<T> { } public class Usage<T> { public void Test() { EventArgs args = new EventArgs(); var foo = args as Foo; // Compiler Error: cannot convert type var bar = args as Bar<T>; // No compiler error } } I discovered

Overriding abstract generic method in Java

浪尽此生 提交于 2020-01-23 16:43:28
问题 Problem outline I'm generifying the better part of my current project's base and I had an idea that I decided to test regarding overriding an abstract method. Here are my test classes in Java: public abstract class Base { public abstract <T extends Base> T test(); } First implementation: public class Inheritor extends Base { @Override public Inheritor test() { return null; } } Second implementation: public class Inheritor2 extends Base { @Override public <T extends Base> T test() { return

Multiple restrictions on generic type based on super and sub classes in java

淺唱寂寞╮ 提交于 2020-01-23 08:29:47
问题 I have a generic list class that implements a particular interface. The list items in the interface also implement the same interface. public abstract class List<T extends SomeInterface> implements SomeInterface { protected LinkedList<T> m_list; ... } So now I want to make a subclass of this list that stays generic but limits the items to objects that implement the SearchListItem interface: public interface SearchListItem { public String getName(); } Here's what I have for the SearchList

What's the difference between a trait's generic type and a generic associated type?

独自空忆成欢 提交于 2020-01-23 05:45:08
问题 This question is asked before generic associated types are available in Rust, although they are proposed and developed. My understanding is that trait generics and associated types differ in the number of types which they can bind to a struct. Generics can bind any number of types: struct Struct; trait Generic<G> { fn generic(&self, generic: G); } impl<G> Generic<G> for Struct { fn generic(&self, _: G) {} } fn main() { Struct.generic(1); Struct.generic("a"); } Associated types bind exactly 1

About Scala generics: cannot find class manifest for element type T

孤街浪徒 提交于 2020-01-23 05:37:05
问题 For a function as below: def reverse[T](a: Array[T]): Array[T] = { val b = new Array[T](a.length) for (i <- 0 until a.length) b(i) = a(a.length -i - 1) b } I am getting "error: cannot find class manifest for element type T" from line 2. Is there anyway to solve this? 回答1: Simply add a context bound ClassManifest to your method declaration: def reverse[T : ClassManifest](a: Array[T]): Array[T] = ... In order to construct an array, the array's concrete type must be known at compile time. This

Check which type of object List<?> contains

孤街浪徒 提交于 2020-01-23 05:23:08
问题 List contains the object type, but I need to check if that object is of type A or B : A a = new A(); B b = new B(); List<A> aL = new ArrayList<A>(); List<B> bL = new ArrayList<B>(); How can I check whether List contains A objects or B objects? Here is the code: SegmentDetailInfo segmentDetailInfo = new SegmentDetailInfo(); segmentDetailInfo.setSeg_Id("1"); SegReqInfoBean segReqInfoBean = new SegReqInfoBean(); segReqInfoBean.setPageName("homepage"); List<SegmentDetailInfo> rhsList1 = new

Generics ambiguity with the &-operator and order

元气小坏坏 提交于 2020-01-23 04:33:26
问题 I have a strange Java generics ambiguity behaviour that I cannot explain: Those 3 methods in class: public static <E extends ClassA & ClassB> void method(E val) {} public static <E extends ClassC & ClassB & ClassA> void method(E val) {} public static <E extends ClassB> void method(E val) {} compile fine. But those not (ambiguity violation): public static <E extends ClassA & ClassB> void method(E val) {} public static <E extends ClassB & ClassC & ClassA> void method(E val) {} public static <E