generics

Is there a way to get GUID from a generic constraint type?

喜夏-厌秋 提交于 2021-01-28 06:42:38
问题 In the sample code below, the Run<T>() displays the values of GUID IFoo and IFoo<T> interfaces: type IBar = interface ['{992E6597-42F1-40F8-B678-C4A86864B030}'] end; IFoo = interface ['{0C589AF8-5727-4EAA-BB41-6D51D70B9D35}'] end; IFoo<T> = interface(IFoo) ['{8FF54F6B-0896-4EA3-85F8-66BA70F9D2DA}'] end; TTest = class public class procedure Run<T: IFoo>; end; class procedure TTest.Run<T>; var LContext: TRttiContext; IFoo_T_TypeInfo: PTypeInfo; IFooTypeInfo: PTypeInfo; begin IFoo_T_TypeInfo :=

Correct usage of generics in event declaration

你离开我真会死。 提交于 2021-01-28 06:41:42
问题 I have a data object which I pass around : public class TimedResult<T> { public ResultRequest Request { get; set; } public T Data { get; set; } public TimeSpan TimeTaken { get; set; } } In my Manager class I have some work done which raises an event : public void Execute(ResultRequest request) { var result = new TimedResult<DataTable>(); // fill data object here OnResult(result); } I have made the OnResult method generic : protected virtual void OnResult<T>(TimedResult<T> result) { if (Result

How would I use an array of generics with a type parameter conforming to a protocol?

北城余情 提交于 2021-01-28 05:33:44
问题 Is there a way to have an array (or any generic type really) of generics with a type parameter conforming to a protocol? protocol MyProtocol {} struct MyStruct<T: MyProtocol> { let myProp: T } // Generic parameter 'T' could not be inferred // Explicitly specify the generic arguments to fix this issue let array1 = [MyStruct]() // Value of protocol type 'MyProtocol' cannot conform to 'MyProtocol'; // only struct/enum/class types can conform to protocols let array2 = [MyStruct<MyProtocol>]() //

Why does the generic Dictionary in .NET not provide a ForEach() method?

廉价感情. 提交于 2021-01-28 05:22:02
问题 After a couple hours of research (on MSDN websites and so on) I didn't manage to find out why the generic Dictionary<TKey, TValue> does not provide a ForEach() method like List<T> does. Could someone please give me an explanation? (I know that it's not hard to implement it as an extension method, a great example can be seen here, I just was wondering whether there might be a particular reason why it's not provided by the .NET libraries in the first place.) Thanks in advance. 回答1: Because it's

Build a Generic Expression Tree .NET Core

二次信任 提交于 2021-01-28 05:16:59
问题 Hello Community i am aware of this might be a possible duplicate. How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>? https://www.strathweb.com/2018/01/easy-way-to-create-a-c-lambda-expression-from-a-string-with-roslyn/ How to create a Expression.Lambda when a type is not known until runtime? Creating expression tree for accessing a Generic type's property There are obviously too many resources. I am still confused though. Could

Multiple enum types list all cases

…衆ロ難τιáo~ 提交于 2021-01-28 05:13:47
问题 can't wrap my head around this and can't find any answers, hope someone could point me in the right direction. I have multiple enums. Some examples(i have a lot more and also more complex enums): enum Fish: String, CaseIterable, Displayable { case goldfish case blueTang = "blue_tang" case shark } enum Tree: String, CaseIterable, Displayable { case coconut case pine case englishOak = "english_oak" } I want to display those in a list with sections. I'm using swiftUI but that probably doesn't

Defining an “mempty”-like function with GHC Generics?

爱⌒轻易说出口 提交于 2021-01-28 04:44:45
问题 I am writing a client library for the Zoho REST API and have a bunch of different record types that have all Maybe a fields, i.e: data Approval = Approval { apDelegate :: Maybe Bool , apApprove :: Maybe Bool , apReject :: Maybe Bool , apResubmit :: Maybe Bool } deriving (Eq, Show, Generic) data ContactSpecialFields = ContactSpecialFields { csfCurrencySymbol :: Maybe Text -- $currency_symbol , csfState :: Maybe Text -- $state , csfProcessFlow :: Maybe Bool -- $process_flow , csfApproved ::

generic collection generation with a generic type

别来无恙 提交于 2021-01-28 04:24:45
问题 Sometimes, I find myself wishing scala collections to include some missing functionality, and it's rather easy "extending" a collection, and provide a custom method. This is a bit more difficult when it comes to building the collection from scratch. Consider useful methods such as .iterate . I'll demonstrate the usecase with a similar, familiar function: unfold . unfold is a method to construct a collection from an initial state z: S , and a function to generate an optional tuple of the next

Is there a way to declare a union type in TypeScript with computed strings based on existing constants?

匆匆过客 提交于 2021-01-28 04:23:56
问题 Let's assume we have following constant: const something = { foo: { bar: { num: 67, str: 'str', }, }, some: { prop: 12, }, topProp: 25, }; The Task: Implement typechecking for following deep property access function /** * @example * getByPath('foo/bar/str'); // returns 'str' * * @example * getByPath('topProp'); // returns 25 * * @example * getByPath('some/prop'); // returns 12 */ const getByPath = (path: ComputedUnionType) => {<unrelated-code-magic>}; // Where type ComputedUnionType = 'foo

Static variable in an abstract generic class

可紊 提交于 2021-01-28 04:17:56
问题 I have a class called cache. It is an generic, abstract class responsible for handling the global cache for forever type extends the class. My question is, if I have a static variable under the base class, will the static variable be unique per extending type or will it be the same for all types that extend Cache. For example the interface: Cache<K, V> private static Cache<K, V> [creates a cache store on first load] static V get(K key); Then I have an implementing class: PersonCache extends