generics

Pass generic type argument through the literal object with different field names per each field if number of fields values is limited by 4

断了今生、忘了曾经 提交于 2020-08-10 18:15:07
问题 What am I doing? I want to make mapping of an object field-by-field to objects of some generic type. I have 4 runtime cases (with runtime check and throwing exception if not matches) that I've used as a properties of original object: false transforms to Smth<unknown> | undefined true transforms to Smth<unknown> [] transforms to Smth<unknown> with 0 or more elements [true] transforms to Smth<unknown> with 1 or more elements Actually that's not very difficult, but I'm not very happy to have

Expression<Func<object, bool>> as Property

前提是你 提交于 2020-08-09 09:32:40
问题 I am trying to refactor some code for a generic repository, that passes in a filter object that will filter data, as well as page, sort etc. Each inheriting Filter (Such as CustomerFilter) has the option of defining its own Expression Filter which will get applied by the base class in the repository. So the customer filter will have properties like this: public string CustomerId { get; set; } public override Expression<Func<object, bool>> Predicate => c => ((Customer)c).Id == CustomerId; Then

How to get enum value of raw type from an enum class and a string in kotlin

别等时光非礼了梦想. 提交于 2020-08-07 06:07:51
问题 I have the following code in java: Enum getEnumValue(Class<?> enumClass, String value) { return Enum.valueOf((Class<Enum>) enumClass, value); } How to rewrite this in Kotlin? Update enumValueOf<>() function is not applicable in this case because I don't know the actual type parameter, I only have a Class<?> object with unknown type ( Class<*> in kotlin) and a name string. The Class is known to be enum: Class.isEnum returns true. Using these two inputs, the java code above allows to obtain the

What is the syntax for OCaml style generic type parameters in F#?

ぐ巨炮叔叔 提交于 2020-08-07 05:30:10
问题 According to this answer, F# supports OCaml style type parameters. The example in the question is: type 'a NestedList = List of 'a NestedList list | Elem of 'a However, I could not find this syntax documented anywhere in the F# documentation and moreover, I cannot get the F# compiler accept the syntax in the answer I gave the link to. This attempt to use multiple parameters are not accepted by the compiler: type ('a * 'b) SomeType = ('a * 'b) This however, works: type ('a , 'b) SomeType = ('a

Map of heterogeneous functions in Typescript

筅森魡賤 提交于 2020-08-06 03:54:17
问题 I want to create a registry of different functions in Typescript. For example I have several functions with different arguments types: const func1 = (p: { x: number }) => { console.log("number: " + p.x); }; const func2 = (p: { s: string }) => { console.log("string: " + p.s); }; I create a registry with them and giving them names: const map = { number: func1, string: func2 }; Then I want to infer following type: { funcName: "number", x: number } | { funcName: "string", s: string } This type

Using parameter that implements multiple interfaces pre-generics

喜夏-厌秋 提交于 2020-08-03 12:10:05
问题 Suppose I have these interfaces: public interface I1 { void foo(); } public interface I2 { void bar(); } and the classes: public class A extends AParent implements I1, I2 { // code for foo and bar methods here } public class B extends BParent implements I1, I2 { // code for foo and bar methods here } public class C extends CParent implements I1 { // code for foo method here } Now, with generics I can have a method like: public <T extends I1 & I2> void method(T param) { param.foo(); param.bar(

Using parameter that implements multiple interfaces pre-generics

对着背影说爱祢 提交于 2020-08-03 12:09:48
问题 Suppose I have these interfaces: public interface I1 { void foo(); } public interface I2 { void bar(); } and the classes: public class A extends AParent implements I1, I2 { // code for foo and bar methods here } public class B extends BParent implements I1, I2 { // code for foo and bar methods here } public class C extends CParent implements I1 { // code for foo method here } Now, with generics I can have a method like: public <T extends I1 & I2> void method(T param) { param.foo(); param.bar(

Using parameter that implements multiple interfaces pre-generics

风流意气都作罢 提交于 2020-08-03 12:08:07
问题 Suppose I have these interfaces: public interface I1 { void foo(); } public interface I2 { void bar(); } and the classes: public class A extends AParent implements I1, I2 { // code for foo and bar methods here } public class B extends BParent implements I1, I2 { // code for foo and bar methods here } public class C extends CParent implements I1 { // code for foo method here } Now, with generics I can have a method like: public <T extends I1 & I2> void method(T param) { param.foo(); param.bar(

Test for equality to the default value

可紊 提交于 2020-07-31 07:50:17
问题 The following doesn't compile: public void MyMethod<T>(T value) { if (value == default(T)) { // do stuff } } Error: Operator '==' cannot be applied to operands of type 'T' and 'T' I can't use value == null because T may be a struct. I can't use value.Equals(default(T)) because value may be null . What is the proper way to test for equality to the default value? 回答1: To avoid boxing for struct / Nullable<T> , I would use: if (EqualityComparer<T>.Default.Equals(value,default(T))) { // do stuff

Is it possible to make an object expose the interface of an type parameter?

故事扮演 提交于 2020-07-29 06:06:30
问题 In C#, is it possible to write something like this: public class MyClass<T> : T where T : class, new() { } I know that the above implementation does not compile, but what I am actually trying to achive is implementing some kind of generic wrapper to an unknown type, so that an client can call the wrapper just as he would call the type, provided by the parameter T , instead of calling it using something like wrapper.Instance.SomeMember() . Thanks in advance! 回答1: This isn't possible. In my