generics

Generic typescript: Generic type from keyof T values

三世轮回 提交于 2021-01-21 11:52:51
问题 How can I infer the result type (TTarget) from TSource and the given property names (keyof TSource)? I've the following function to copy defined properties to a new object: export declare type PropertyNamesOnly<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]; CopyProps<TSource, TTarget>(source: TSource, ...props: PropertyNamesOnly<TSource>[]): TTarget { const result: any = {}; for (const prop of props) { result[prop] = source[prop]; } return result; } Now I can use it like

What does the <% operator mean in Scala generics? [duplicate]

空扰寡人 提交于 2021-01-21 08:26:59
问题 This question already has an answer here : What are Scala context and view bounds? (1 answer) Closed 15 days ago . In specs2 there is a method called Around, documented here that has the following example: object http extends Around { def around[T <% Result](t: =>T) = openHttpSession("test") { t // execute t inside a http session } } The source for this code can be found here. I'm curious what the <% operator means in this context? EDIT: here is a solid answer on this subject, What are Scala

What does the <% operator mean in Scala generics? [duplicate]

流过昼夜 提交于 2021-01-21 08:25:23
问题 This question already has an answer here : What are Scala context and view bounds? (1 answer) Closed 15 days ago . In specs2 there is a method called Around, documented here that has the following example: object http extends Around { def around[T <% Result](t: =>T) = openHttpSession("test") { t // execute t inside a http session } } The source for this code can be found here. I'm curious what the <% operator means in this context? EDIT: here is a solid answer on this subject, What are Scala

What does the <% operator mean in Scala generics? [duplicate]

故事扮演 提交于 2021-01-21 08:24:13
问题 This question already has an answer here : What are Scala context and view bounds? (1 answer) Closed 15 days ago . In specs2 there is a method called Around, documented here that has the following example: object http extends Around { def around[T <% Result](t: =>T) = openHttpSession("test") { t // execute t inside a http session } } The source for this code can be found here. I'm curious what the <% operator means in this context? EDIT: here is a solid answer on this subject, What are Scala

dart How to get an enum with index?

[亡魂溺海] 提交于 2021-01-21 07:28:44
问题 I defined an enum: enum TestEnum { test1, test2, } and I want make an enum with index: E buildEnum<E extends ?????????????????>(int index) { try { return E.values[index]; } catch(e) { return null; } } I don't know the type of enum. 回答1: enum A { a1, a2, a3} A.values[index] 回答2: You cannot make static accesses on a type parameter, so this cannot work. There is no way except reflection ( dart:mirrors ) to go from a value to a static member of its type. 回答3: I know what you mean, but it doesn't

How to mock generic method in Java with Mockito?

你说的曾经没有我的故事 提交于 2021-01-21 04:18:44
问题 How can we mock the IRouteHandlerRegistry ? The error is Cannot resolve method thenReturn(IHandleRoute<TestRoute>) public interface RouteDefinition { } public class TestRoute implements RouteDefinition { } public interface IHandleRoute<TRoute extends RouteDefinition> { Route getHandlerFor(TRoute route); } public interface IRouteHandlerRegistry { <TRoute extends RouteDefinition> IHandleRoute<TRoute> getHandlerFor(TRoute route); } @Test @SuppressWarnings("unchecked") public void test() { // in

Passing integer array to method that uses a generic array of elements? [duplicate]

会有一股神秘感。 提交于 2021-01-20 12:00:29
问题 This question already has answers here : Java: Array of primitive data types does not autobox (10 answers) Closed 5 years ago . I have the following class public class TestAlgorithm<E extends Comparable<? super E>> { public void testing(E[] array) { for(int i = 0; i<= array.length; i++) { ... // processing code (not important here) } } } in my main application class class I have this... public static void main(String[] args) { int [] test = {3,7,8,5,2,1,9,5,4}; TestAlgorithm<Integer> myAlgo =

Passing integer array to method that uses a generic array of elements? [duplicate]

*爱你&永不变心* 提交于 2021-01-20 12:00:10
问题 This question already has answers here : Java: Array of primitive data types does not autobox (10 answers) Closed 5 years ago . I have the following class public class TestAlgorithm<E extends Comparable<? super E>> { public void testing(E[] array) { for(int i = 0; i<= array.length; i++) { ... // processing code (not important here) } } } in my main application class class I have this... public static void main(String[] args) { int [] test = {3,7,8,5,2,1,9,5,4}; TestAlgorithm<Integer> myAlgo =

Generic class type parameter detail at runtime

China☆狼群 提交于 2021-01-19 22:17:01
问题 Libraries like Jackson can create objects from JSON if we provide enough information about generics. In Jackson, we can do Class<?>[] parameterTypes; JavaType type = objectMapper.getTypeFactory().constructParametricType(ObjectClass, parameterTypes); objectMapper.readValue(json, type); In java, a Generic class can be defined in many ways, like one generic class having another generic class and that can have another generic class, for simple illustration consider these three classes. public

Generic class type parameter detail at runtime

落花浮王杯 提交于 2021-01-19 22:05:19
问题 Libraries like Jackson can create objects from JSON if we provide enough information about generics. In Jackson, we can do Class<?>[] parameterTypes; JavaType type = objectMapper.getTypeFactory().constructParametricType(ObjectClass, parameterTypes); objectMapper.readValue(json, type); In java, a Generic class can be defined in many ways, like one generic class having another generic class and that can have another generic class, for simple illustration consider these three classes. public