generics

Simple Generic Function in Kotlin Fails

孤人 提交于 2020-04-30 10:05:10
问题 Here's a simple generic function in Kotlin: fun <T> twice(x: T) : T { return 2 * x } Attempting to build this (either in a project or REPL) results in the following errors: error: none of the following functions can be called with the arguments supplied: public final operator fun times(other: Byte): Int defined in kotlin.Int public final operator fun times(other: Double): Double defined in kotlin.Int public final operator fun times(other: Float): Float defined in kotlin.Int public final

How to get valueOf & values of an Enum and call methods on an interface it implements when defined as a generic class param

房东的猫 提交于 2020-04-30 07:14:27
问题 I want to convert String s to Enum s for a set of enum types. I use an interface to mark what set of enums to convert and define a couple of methods used during conversion: public interface SymbolEnum { String getCode(); String getDescription(); } Here's an example of one of the Enum types that I want to derive from a String : @RequiredArgsConstructor // using lombok public enum Element implements SymbolEnum { IRON("FE", "iron"), HYDROGEN("H", "hydrogen"), @Getter private final String code;

How to get valueOf & values of an Enum and call methods on an interface it implements when defined as a generic class param

三世轮回 提交于 2020-04-30 07:14:13
问题 I want to convert String s to Enum s for a set of enum types. I use an interface to mark what set of enums to convert and define a couple of methods used during conversion: public interface SymbolEnum { String getCode(); String getDescription(); } Here's an example of one of the Enum types that I want to derive from a String : @RequiredArgsConstructor // using lombok public enum Element implements SymbolEnum { IRON("FE", "iron"), HYDROGEN("H", "hydrogen"), @Getter private final String code;

How can I access T from a Generic[T] instance early in its lifecycle?

可紊 提交于 2020-04-30 06:35:07
问题 Oh, it feels so contrived to ask now that I've resigned myself to an alternative approach, but Python's typing module has me mystified. I know I can do this: import typing T = typing.TypeVar("T") class MyGenericClass(Generic[T]): def a_method(self): print(self.__orig_class__) MyOtherGeneric[SomeBaseClass]().a_method() to print SomeBaseClass . Probably, I will just stick with that ability to achieve what I am ultimately trying to do (modify functionality based on T ), but I am now stuck

C# - Specifying type parameters of super-class in sub-classes?

懵懂的女人 提交于 2020-04-18 06:10:12
问题 I am trying to do the following in C#. public class Parent<T> where T : Parent<???> { public T Prop { get; set; } } public class Child : Parent<Child> { } How can I do it? 回答1: This works fine: public class Parent<T> where T : Parent<T> { public T Prop { get; set; } } public class Child : Parent<Child> { } Do be careful with this as c# does not enforce a true Parent / Child relationship. For example, given the above code, it is also legal for me to then do this: public class Stranger : Parent

App Crashing with error: generic parameter 'T' could not be inferred

旧巷老猫 提交于 2020-04-18 05:45:40
问题 I'm trying to get custom object which is hashable from UserDefault. My custom model is defined below: class WorkerProfileResponse: Mappable, Hashable{ static func == (lhs: WorkerProfileResponse, rhs: WorkerProfileResponse) -> Bool { return lhs.id == rhs.id } var hashValue: Int{ return self.id! } var id, loginStatus, lastLogin, lastActive: Int? var username, email, mobileNumber: String? var userCategories: [String]? var userSubCategories: [String]? var biometricToken: String? var accessToken:

How to get class of generic type in Spring

你。 提交于 2020-04-16 19:46:58
问题 I have a generics class DAO in my spring project,I have to get class of generic T.I know the pure java solution: class Foo<T> { final Class<T> typeParameterClass; public Foo(Class<T> typeParameterClass) { this.typeParameterClass = typeParameterClass; } public void bar() { // you can access the typeParameterClass here and do whatever you like } } But,in spring project,I have to get the Foo from the "ApplicationContext",I can't get Foo by: Foo<ClassName> foo = new Foo<ClassName>(ClassName.class

How to get class of generic type in Spring

纵饮孤独 提交于 2020-04-16 19:40:50
问题 I have a generics class DAO in my spring project,I have to get class of generic T.I know the pure java solution: class Foo<T> { final Class<T> typeParameterClass; public Foo(Class<T> typeParameterClass) { this.typeParameterClass = typeParameterClass; } public void bar() { // you can access the typeParameterClass here and do whatever you like } } But,in spring project,I have to get the Foo from the "ApplicationContext",I can't get Foo by: Foo<ClassName> foo = new Foo<ClassName>(ClassName.class

How to implement a pluck function in TypeScript?

丶灬走出姿态 提交于 2020-04-16 06:19:09
问题 I often need to pluck properties out of an object const obj = {a:1, b: 2, c: 3}; const plucked = pluck(obj, 'a', 'b'); // {a: 1, b:2} However, this is not easy to do in TypeScript if you want type safety because I can't implement a function with the following signature in TypeScript, which I found on TypeScript's manual. declare function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>; I couldn't get a version that compiled, here's a version I tried: function pluck<T, K extends

Decoding object with multiple constuctors with separated tags

▼魔方 西西 提交于 2020-04-16 03:28:19
问题 I have data in form of pairs of two strings, where first is the key identifying shape of the JSON delivered as the second one. fooooo {"a": 123} barrrr {"a": 123, "b": 123} fooooo {"a": 123} I would like to parse it to same data type, based on the fopooo , baasdasda1 , etc : data Test = Foo { a :: Int , b :: Int } | Bar { a :: Int } deriving (Show, Generic) In the Aeson there is a tag feature, but it seems to require presence of the tag inside the object. Is there some way to handle it like