generics

Calling a static method from a generic constraint Dart

孤者浪人 提交于 2020-08-25 04:59:39
问题 I'm trying to call a static method from a generic type I receive. Is that even possible? Furthermore, I apply a Type constraint in order to only manipulate the object from its parent class. Here is a short example of what I'm trying to achieve: class A { static func() { print("A"); } } class B extends A { static func() { print("B"); } } concret<T extends A>() { T.func(); // I expected a print('B') } main() { concret<B>(); } 回答1: No, it's not possible. Dart static method invocations are

Switch based on generic argument type

一笑奈何 提交于 2020-08-24 08:34:17
问题 In C# 7.1 the below is valid code: object o = new object(); switch (o) { case CustomerRequestBase c: //do something break; } However, I want to use the pattern switch statement in the following scenario: public T Process<T>(object message, IMessageFormatter messageFormatter) where T : class, IStandardMessageModel, new() { switch (T) { case CustomerRequestBase c: //do something break; } } The IDE gives me the error "'T' is a type, which is not valid in the given context" Is there an elegant

Switch based on generic argument type

安稳与你 提交于 2020-08-24 08:31:32
问题 In C# 7.1 the below is valid code: object o = new object(); switch (o) { case CustomerRequestBase c: //do something break; } However, I want to use the pattern switch statement in the following scenario: public T Process<T>(object message, IMessageFormatter messageFormatter) where T : class, IStandardMessageModel, new() { switch (T) { case CustomerRequestBase c: //do something break; } } The IDE gives me the error "'T' is a type, which is not valid in the given context" Is there an elegant

What is the difference between <T> and <T extends Object> in java? [duplicate]

北城以北 提交于 2020-08-24 06:38:55
问题 This question already has answers here : What's the difference between <?> and <? extends Object> in Java Generics? (3 answers) Closed 5 years ago . In Java generics, What is the advantage of using class GenericStack<T extends Object> {} over class GenericStack<T>{} . I have implemented a generic stack using both of the above approaches but unable to trace out the difference. Help me to understand this. 回答1: There's no difference. <T> and <T extends Object> are equivalent. 来源: https:/

What is the difference between <T> and <T extends Object> in java? [duplicate]

穿精又带淫゛_ 提交于 2020-08-24 06:38:18
问题 This question already has answers here : What's the difference between <?> and <? extends Object> in Java Generics? (3 answers) Closed 5 years ago . In Java generics, What is the advantage of using class GenericStack<T extends Object> {} over class GenericStack<T>{} . I have implemented a generic stack using both of the above approaches but unable to trace out the difference. Help me to understand this. 回答1: There's no difference. <T> and <T extends Object> are equivalent. 来源: https:/

Generic BiFunction that accepts generic wildcard

徘徊边缘 提交于 2020-08-24 04:22:06
问题 I have Enum and want to have a generic function that could accept any class that implements a maker interface. public interface IComponentDataExporter { // Marker Interface } public class ComponentsDataExporter implements IComponentDataExporter { public ComponentTeaser populateFeatured(ComponentTeaserConfiguration componentConfig) { return null; } } public class BussinessComponentsDataExporter implements IComponentDataExporter { // methods } public enum ComponentTypeEnum { FEATURED

Update parent and child collections on generic repository with EF Core

自古美人都是妖i 提交于 2020-08-21 05:35:21
问题 Say I have a Sale class: public class Sale : BaseEntity //BaseEntity only has an Id { public ICollection<Item> Items { get; set; } } And an Item class: public class Item : BaseEntity //BaseEntity only has an Id { public int SaleId { get; set; } public Sale Sale { get; set; } } And a Generic Repository (update method): public async Task<int> UpdateAsync<T>(T entity, params Expression<Func<T, object>>[] navigations) where T : BaseEntity { var dbEntity = _dbContext.Set<T>().Find(entity.Id); var

Update parent and child collections on generic repository with EF Core

纵然是瞬间 提交于 2020-08-21 05:34:09
问题 Say I have a Sale class: public class Sale : BaseEntity //BaseEntity only has an Id { public ICollection<Item> Items { get; set; } } And an Item class: public class Item : BaseEntity //BaseEntity only has an Id { public int SaleId { get; set; } public Sale Sale { get; set; } } And a Generic Repository (update method): public async Task<int> UpdateAsync<T>(T entity, params Expression<Func<T, object>>[] navigations) where T : BaseEntity { var dbEntity = _dbContext.Set<T>().Find(entity.Id); var

What is out keyword in kotlin

心已入冬 提交于 2020-08-20 18:07:17
问题 I am not able to understand and I couldn't find the meaning of out keyword in kotlin. You can check example here: List<out T> If any one can explain the meaning of this. It would be really appreciated. 回答1: With this signature: List<out T> you can do this: val doubleList: List<Double> = listOf(1.0, 2.0) val numberList: List<Number> = doubleList which means T is covariant : when a type parameter T of a class C is declared out , C<Base> can safely be a supertype of C<Derived> . This is contrast

Unable to convert List<List<int>> to return type IList<IList<int>>

两盒软妹~` 提交于 2020-08-19 11:42:20
问题 For level order traversal why does this exception occur? Following exception occurs: Cannot implicitly convert type ' System.Collections.Generic.List<System.Collections.Generic.List<int>> ' to ' System.Collections.Generic.IList<System.Collections.Generic.IList<int>> '. An explicit conversion exists (are you missing a cast?) public IList<IList<int>> LevelOrder(TreeNode root) { var result = new List<List<int>>(); var que = new Queue<TreeNode>(); //if(root==null) return result; que.Enqueue(root)