generics

Swift Casting Generic to Optional with a nil value causes fatalError

♀尐吖头ヾ 提交于 2020-01-05 02:58:10
问题 Using Swift 2, in my contrived example I am converting a String to an Int or more specifically an Int or an Int? using a generic. In the case where the Int? should be nil the cast will fail with a fatalError: fatal error: unexpectedly found nil while unwrapping an Optional value These look like they may be similar/duplicate questions: Swift - casting a nil core data string as an optional value Swift: detecting an unexpected nil value in a non-optional at runtime: casting as optional fails My

How to Configure Log4Net Custom Object Renderer for Generic Objects?

我的梦境 提交于 2020-01-05 02:29:48
问题 I have a generic object: MyGenericObject(of T), how do I register a custom renderer in log4net in the config file? For example, if this was a normal object the following would work: <renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject, MyClassLibrary" /> However I don't know how to write this for Generics, I attempted this: <renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject(Of MyNonGenericObject), MyClassLibrary" /> but this

Create instance of Spring´s ParameterizedTypeReference in Kotlin

这一生的挚爱 提交于 2020-01-04 23:05:47
问题 I am trying to learn Kotlin, and test how it works with spring boot. My application is using a mongo database to store data and I have a Jersey resource for retrieving data. I am testing it using spring-boot-test and RestTestTemplate . The RestTestTemplate has an exchange method which takes a ParameterizedTypeReference . This class has a protected constructor. So the only way I managed to use it from Kotlin was like this: class ListOfPeople : ParameterizedTypeReference<List<Person>>() Here is

Generic type in generic collection

北城以北 提交于 2020-01-04 17:51:09
问题 I have generic type that looks like: public class GenericClass<T, U> where T : IComparable<T> { // Class definition here } I then have a collection of these instances. What is the cleanest way to pass through the type constraints? public class GenericCollection<V> where V : GenericClass<T, U> // This won't compile { private GenericClass<T, U>[] entries; public V this[index] { get{ return this.entries[index]; } } } Is there perhaps a better way to design this? I think that specifying

Get list of all items of a generic type from list which could contain other types

倖福魔咒の 提交于 2020-01-04 16:56:09
问题 Okay, this is tricky. Say I have a list: List<Foo> MyList; Foo is a common abstract class which is used for all components in the system. Some of these component classes are generic: public class Bar<T> : Foo { public void MethodCommonToAllBar() { ... } } And some classes may extend the generic ones: public class Baz : Bar<int> public class Bat : Bar<string> I'd like a way to extract from MyList a set of all objects of type Bar<anything> , so that I may call MethodCommonToAllBar() on each of

Get list of all items of a generic type from list which could contain other types

你离开我真会死。 提交于 2020-01-04 16:55:50
问题 Okay, this is tricky. Say I have a list: List<Foo> MyList; Foo is a common abstract class which is used for all components in the system. Some of these component classes are generic: public class Bar<T> : Foo { public void MethodCommonToAllBar() { ... } } And some classes may extend the generic ones: public class Baz : Bar<int> public class Bat : Bar<string> I'd like a way to extract from MyList a set of all objects of type Bar<anything> , so that I may call MethodCommonToAllBar() on each of

Null (?) Operator returns incorrect value when using generic type constructor. VB.NET

依然范特西╮ 提交于 2020-01-04 15:11:34
问题 I am experiencing some weird behavior when using Generic Types and null operator. Why does obj2.CurrentDate return a date value that appears to be incorrect when using the ? (short hand). If I long hand the null operator (if) in the property then it returns the correct and expected value. I thought the ? was the equivalent to the if(expression, returnIfTrue, returnIfFalse). Also, if the generic type constructor is removed, then the null operator works as expected. Why is this? Return If

Null (?) Operator returns incorrect value when using generic type constructor. VB.NET

廉价感情. 提交于 2020-01-04 15:11:14
问题 I am experiencing some weird behavior when using Generic Types and null operator. Why does obj2.CurrentDate return a date value that appears to be incorrect when using the ? (short hand). If I long hand the null operator (if) in the property then it returns the correct and expected value. I thought the ? was the equivalent to the if(expression, returnIfTrue, returnIfFalse). Also, if the generic type constructor is removed, then the null operator works as expected. Why is this? Return If

Delphi function generic

烈酒焚心 提交于 2020-01-04 14:15:07
问题 I would like to create a generic function. I'm novice in generic. I've 3 private lists of different type. I want a public generic method for return 1 item of the list. I've the code below. (I have it simplifie) TFilter = class private FListFilter : TObjectList<TFilterEntity>; FListFilterDate : TObjectList<TFilterDate>; FListFilterRensParam : TObjectList<TFilterRensParam>; public function yGetFilter<T>(iIndice : integer) : T; .... function TFilter .yGetFilter<T>(iIndice : integer) : T; begin

How to reference the nested type of a nested template parameter inside a class?

你说的曾经没有我的故事 提交于 2020-01-04 13:46:32
问题 This is a followup to this question. Essentially, I have the following classes: public class Parent<B> { public B b; public Parent(B b) { this.b = b; } } public class Child<B> extends Parent<B> { public Child(B b) { super(b); } } And I'd like to have another class that references them: public class Foo<ParentType<B> extends Parent<B>, B> { // ^ syntax error here: > expected public ParentType<B> parent; public B otherItem; public Foo(ParentType<B> parent, B otherItem) { this.parent = parent;