generics

Bounded-wildcard related compiler error

淺唱寂寞╮ 提交于 2020-01-08 18:01:15
问题 I am wondering what is wrong with this code: Map <? extends String, ? extends Integer> m = null; Set<Map.Entry<? extends String, ? extends Integer>> s = m.entrySet(); The compiler complains with the error message: Type mismatch: cannot convert from Set<Map.Entry<capture#1-of ? extends String,capture#2-of ? extends Integer>> to Set<Map.Entry<? extends String,? extends Integer>> What should the type of s be? Eclipse suggests Set<?> but I am trying to get more specific than that. 回答1: This issue

How to pass objects properties into a generic function

感情迁移 提交于 2020-01-07 10:52:14
问题 I am trying to create a function that takes in a object property and returns back object property. How to get the property values into this specific Function, so that this function only takes takes in the object's specific property and not the entire object? class Program { public T MapFrom<T,V>(T SourceObject.property, V DestinationObject.Property) //Not able to achieve this// { // Code to Map Property } // Here I want to specifically pass only one property of Object , not the entire one

How to pass objects properties into a generic function

自作多情 提交于 2020-01-07 10:51:13
问题 I am trying to create a function that takes in a object property and returns back object property. How to get the property values into this specific Function, so that this function only takes takes in the object's specific property and not the entire object? class Program { public T MapFrom<T,V>(T SourceObject.property, V DestinationObject.Property) //Not able to achieve this// { // Code to Map Property } // Here I want to specifically pass only one property of Object , not the entire one

How to pass objects properties into a generic function

南楼画角 提交于 2020-01-07 10:50:39
问题 I am trying to create a function that takes in a object property and returns back object property. How to get the property values into this specific Function, so that this function only takes takes in the object's specific property and not the entire object? class Program { public T MapFrom<T,V>(T SourceObject.property, V DestinationObject.Property) //Not able to achieve this// { // Code to Map Property } // Here I want to specifically pass only one property of Object , not the entire one

Understanding best use of Java Generics in this example case

為{幸葍}努か 提交于 2020-01-07 06:22:25
问题 Let's say I have a manufacturing scheduling system, which is made up of four parts: There are factories that can manufacture a certain type of product and know if they are busy: interface Factory<ProductType> { void buildProduct(ProductType product); boolean isBusy(); } There is a set of different products, which (among other things) know in which factory they are built: interface Product<ActualProductType extends Product<ActualProductType>> { Factory<ActualProductType> getFactory(); } Then

Guava cache generics error

醉酒当歌 提交于 2020-01-07 04:36:29
问题 Magical error appears when trying to create guava cache: import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import java.util.concurrent.ConcurrentMap; public class Main { private static ConcurrentMap<Long, Object> cache = CacheBuilder .newBuilder() .build(new CacheLoader<Long, Object>() { @Override public Object load(Long key) throws Exception { return null; } }).asMap(); } java compile error: Error:(17, 21) java: C:\JavaWorkspace\untitled\src\...\Main

how do I write a function that can iterate over a generic type in a typesafe way [closed]

守給你的承諾、 提交于 2020-01-07 03:51:12
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I want to write a function that can input an iterator over a given generic type in a typesafe way. One possible use case would be writing a function like accumulate/map/fold: #include <iterator> #include <vector> #include <functional> template <typename V, typename K> K accumulate( std::function<K

Boxing error with generic function

一世执手 提交于 2020-01-07 03:40:23
问题 If I comment out my static class this compiles fine. However i'd like to have the static class working so I can use the first commented out line in main. My error is error CS0314: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'DateTest.Range'. There is no boxing conversion or type parameter conversion from 'T' to 'System.IComparable'. My source is using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading

Swift compiler unable to resolve correct overload of + operator for arrays in generic function

喜欢而已 提交于 2020-01-07 03:19:10
问题 I've had several problems with the plus operator so I decided to investigate and ended up with the following minimal example. When I compile func f<T>(t: T) -> [T] { return [t] + [t] } everything is fine. The compiler chooses this overload of the plus operator: public func +<RRC1 : RangeReplaceableCollection, RRC2 : RangeReplaceableCollection where RRC1.Iterator.Element == RRC2.Iterator.Element> (lhs: RRC1, rhs: RRC2) -> RRC1 However, when I add another + to the game, I get this: func f<T>(t:

Get Type of ParameterizedType from generic

旧时模样 提交于 2020-01-07 02:28:06
问题 I cannot understand fully how reflect works, so I'm having some trouble trying to register a custom type adapter for my GsonBuilder . I need to register a type adapter for a List<Something> type. This is a working code: gsonBuilder.registerTypeAdapter( new TypeToken<List<MyClass>>() { }.getType(), new ListDeserializer(MyClass.class) ); The thing is that I have a lot of classes to register my type adapter, and I have a ListDeserializer and SingleDeserializer adapters. I'm trying to go generic