generics

C# equivalent of Java's List<? extends MyFancyClass>

大憨熊 提交于 2021-02-11 08:57:56
问题 I don't believe I saw this anywhere (I'm not sure how to word the question really so that may be why) so if it's already been answered I apologise. I'm currently writing an application that has an event API that third parties can hook into, below is the code I'm using to fire the events. // Events.cs from the API project public interface IEventListener { void Handle(IEvent @event); } public abstract class EventManager { public abstract void Register(PluginBase plugin, IEventListener listener)

Gson desearilize list and class, how does erasure work here?

大兔子大兔子 提交于 2021-02-11 00:22:14
问题 I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static <T> String toJson(T object) { if (object == null) { return null; } return gson.toJson(object); } public static <T> T fromJson(String json, Class<T> className) { if (StringUtils.isEmpty(json)) { return null; } T object = gson.fromJson(json, className); return object; } public static <T>

Gson desearilize list and class, how does erasure work here?

筅森魡賤 提交于 2021-02-11 00:19:50
问题 I intend to write a generic method to convert a json list into it's specific list with class. This is the generic json parser: public class JsonParserUtils { private static Gson gson = new Gson(); public static <T> String toJson(T object) { if (object == null) { return null; } return gson.toJson(object); } public static <T> T fromJson(String json, Class<T> className) { if (StringUtils.isEmpty(json)) { return null; } T object = gson.fromJson(json, className); return object; } public static <T>

How do I specify the expected result of a `std::ops::Mul` in a trait bound?

守給你的承諾、 提交于 2021-02-10 18:01:35
问题 I have: use std::ops::{Add, Div, Mul, Neg, Sub}; pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64> + Div<f64> + Sized { fn dot(&self, other: &Self) -> f64; fn magnitude(&self) -> f64; } fn g<T: Hilbert>(x: T) -> f64 { return (x * 2.0).dot(x); } ...which yields: error[E0599]: no method named `dot` found for type `<T as std::ops::Mul<f64>>::Output` in the current scope --> src/main.rs:9:22 | 9 | return (x * 2.0).dot(x); | ^^^ | = help: items from traits can only be used if the trait is

Generic method return type - compilation error [duplicate]

蹲街弑〆低调 提交于 2021-02-10 16:00:27
问题 This question already has an answer here : Java generics : Type mismatch: cannot convert from Integer to K (1 answer) Closed 3 years ago . Given this code sample: class A { } public class TestGenerics { private static <T extends A> T failsToCompile() { return new A(); } private static <T extends A> T compiles(T param) { return param; } } how come the first method doesn't compile, but the second one does? The error is: Incompatible types. Required: T. Found: A Essentially what I'm trying to

Generic method return type - compilation error [duplicate]

余生颓废 提交于 2021-02-10 15:56:59
问题 This question already has an answer here : Java generics : Type mismatch: cannot convert from Integer to K (1 answer) Closed 3 years ago . Given this code sample: class A { } public class TestGenerics { private static <T extends A> T failsToCompile() { return new A(); } private static <T extends A> T compiles(T param) { return param; } } how come the first method doesn't compile, but the second one does? The error is: Incompatible types. Required: T. Found: A Essentially what I'm trying to

Generic method return type - compilation error [duplicate]

邮差的信 提交于 2021-02-10 15:55:59
问题 This question already has an answer here : Java generics : Type mismatch: cannot convert from Integer to K (1 answer) Closed 3 years ago . Given this code sample: class A { } public class TestGenerics { private static <T extends A> T failsToCompile() { return new A(); } private static <T extends A> T compiles(T param) { return param; } } how come the first method doesn't compile, but the second one does? The error is: Incompatible types. Required: T. Found: A Essentially what I'm trying to

Inherit from a class parametrized by an inner type

末鹿安然 提交于 2021-02-10 14:14:35
问题 I would like to have a class B that inherits from a generic class A that is parametrized by an inner type of B . Specifically, I would like this (minimized example): class A[T] class B extends A[T] { class T } Written like this, the compiler does not accept it. Is there any way to specify this inheritance relationship? (Using some different syntax, or some tricks.) If not, what would be an official reference documenting that this is not possible? Notes: Yes, I want T to be an inner class. I

Incompatible types: Class<Bar> cannot be converted to Class<CAP#1> where CAP#1 is a fresh-type variable

回眸只為那壹抹淺笑 提交于 2021-02-10 13:34:31
问题 I encountered something that bugs me when I wrote some code. I gathered the two examples in the code sample below. The cls1 line uses a lambda expression but doesn't compile, while the cls2 line uses a method references and compiles. I know that if I'm using non generic objects, I have no issues there, but here, I'm using generics, and, more specifically, wildcards. import java.lang.annotation.*; import java.util.Optional; public class MCVE { static class Foo {} static class Bar extends Foo {

Python Dictionary with generic keys and Callable[T] values

独自空忆成欢 提交于 2021-02-10 12:42:54
问题 I have some named tuples: JOIN = NamedTuple("JOIN", []) EXIT = NamedTuple("EXIT", []) I also have functions to handle each type of tuple with: def handleJoin(t: JOIN) -> bool: pass def handleExit(t: EXIT) -> bool: pass What I want to do is create a dictionary handleTuple so I can call it like so: t: Union[JOIN, EXIT] = #an instance of JOIN or EXIT result: bool result = handleTuple[type(t)](t) What I cannot figure out is how to define said dictionary. I tried defining a generic T : T = TypeVar