generics

Storing and calling generically-typed delegates

爷,独闯天下 提交于 2021-02-07 12:14:43
问题 What I'm looking for is probably not going to be possible without resorting to reflection. If that's the case, I'd still want to know the best way to pull it off. Essentially, this is what I want my code to look like: var instance = new MyClass(); instance.Add<int, string>(x => x.ToString()); instance.Add<string, Warehouse>(x => Warehouse.LookupByName(x)); instance.Add<Warehouse, IList<Supplier>>(x => x.Suppliers()); instance.Chain(3); // should call each lambda expression in turn My question

Apparent type violation, but compiles [duplicate]

早过忘川 提交于 2021-02-07 11:30:10
问题 This question already has answers here : String gets assigned to a List without a compilation error [duplicate] (1 answer) Why can this generic method with a bound return any type? (1 answer) Generic return type upper bound - interface vs. class - surprisingly valid code (2 answers) Closed 3 years ago . Why does the below snippet compile ? OtherInterface does not extends Concrete so I would have bet a kidney that this wouldn't compile. But it does. public class Test { public static interface

Does Java fail to deduce the generic type parameter when using the ternary operator (`?`)?

你。 提交于 2021-02-07 11:19:38
问题 Why is the compiler able to determine the generic type parameter for an assignment, but not for the ternary operator ( ? )? I have a question regarding the compiler being able to deduce the generic type parameter in case of a "direct" assignment but failing in case of the ternary operator ( ? ). My examples uses Guava's Optional class, to make my point, but I think the underlying issue is generic and not restricted to Optional . Optional has a generic function absent() : public static <T>

Java cooperating generic classes: can we avoid unchecked cast?

浪尽此生 提交于 2021-02-07 11:13:33
问题 Edit: Sorry; eager to give you a minimal example, I didn’t provide enough information about the requirements in my first version of this question. I have two abstract generic classes. They cooperate and hence depend on each other. Occasionally one needs to pass this to the other. I am trying to find a type safe way to do this. public abstract class AbstractA<T extends AbstractB<? extends AbstractA<T>>> { protected void foo() { T aB = createB(); aB.setA(this); } /** factory method */ abstract

Generics and inheritance in Java library used from Kotlin

痴心易碎 提交于 2021-02-07 10:25:44
问题 I'm having problem implementing a Kotlin class which uses Java-defined generics/interfaces classes from MockMvc library. Library classes: public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder { // ... <T extends B> T defaultRequest(RequestBuilder requestBuilder); // ... } @FunctionalInterface public interface MockMvcBuilderCustomizer { /** * Customize the given {@code builder}. * @param builder the builder */ void customize

Generics and inheritance in Java library used from Kotlin

夙愿已清 提交于 2021-02-07 10:25:23
问题 I'm having problem implementing a Kotlin class which uses Java-defined generics/interfaces classes from MockMvc library. Library classes: public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder { // ... <T extends B> T defaultRequest(RequestBuilder requestBuilder); // ... } @FunctionalInterface public interface MockMvcBuilderCustomizer { /** * Customize the given {@code builder}. * @param builder the builder */ void customize

How do you get the name of a generic class using reflection?

半城伤御伤魂 提交于 2021-02-07 09:59:45
问题 How do you get the name of a generic class using reflection eg public class SomeGenericClass<T> { } SomeGenericClass<int> test = new SomeGenericClass<int>(); test.GetType().Name returns "SomeGenericClass'1" How do I get it to return "SomeGenericClass" without the '1? 回答1: How about just the following? test.GetType().Name.Split('\'')[0] It works on non-generic classes too. 回答2: The '1 is part of the name, because, for example, List<T> and List (if I created such a class) are different classes.

How to Save a Generic Measurement<Unit> in Core Data?

十年热恋 提交于 2021-02-07 09:28:57
问题 How does one save and retrieve a generic Measurement in Core Data? What I'm looking to do is save either a Measurement<UnitMass> or a Measurement<UnitVolume> . As can be seen in the image below CoreData is set to accept a Generic Measurement<Unit> However I'm getting an error when I go to set the value of the measure. Saying that I'm not allowed to do this. I thought the purpose of generics was to support uses like this. What am i missing? 回答1: The error about mismatched types isn't the

unmarshal generic json in Go

你说的曾经没有我的故事 提交于 2021-02-07 08:45:05
问题 I'm a new Go programmer (From Java) and I would like to reproduce a generic way which is esay to use in Java. I want to create some function which allow me to do an Unmarshal on a JSON string in order to avoid code duplicity. This is my current code which is not working : type myStruct1 struct { id string name string } func (obj myStruct1) toString() string { var result bytes.Buffer result.WriteString("id : ") result.WriteString(obj.id) result.WriteString("\n") result.WriteString("name : ")

unmarshal generic json in Go

好久不见. 提交于 2021-02-07 08:42:11
问题 I'm a new Go programmer (From Java) and I would like to reproduce a generic way which is esay to use in Java. I want to create some function which allow me to do an Unmarshal on a JSON string in order to avoid code duplicity. This is my current code which is not working : type myStruct1 struct { id string name string } func (obj myStruct1) toString() string { var result bytes.Buffer result.WriteString("id : ") result.WriteString(obj.id) result.WriteString("\n") result.WriteString("name : ")