generics

Collapsing a discriminated union - derive an umbrella type with all possible key-value combinations from the union

北城以北 提交于 2021-02-20 19:23:52
问题 I have a discriminated union, for example: type Union = { a: "foo", b: string, c: number } | {a: "bar", b: boolean } I need to derive a type that includes all potential properties, assigned with types that may be found on any member of Union , even if only defined on some - in my example: type CollapsedUnion = { a: "foo" | "bar", b: string | boolean, c: number | undefined } How can I make a generic that derives such collapsed unions? I need a generic that supports unions of any size. Similar

Collapsing a discriminated union - derive an umbrella type with all possible key-value combinations from the union

﹥>﹥吖頭↗ 提交于 2021-02-20 19:23:07
问题 I have a discriminated union, for example: type Union = { a: "foo", b: string, c: number } | {a: "bar", b: boolean } I need to derive a type that includes all potential properties, assigned with types that may be found on any member of Union , even if only defined on some - in my example: type CollapsedUnion = { a: "foo" | "bar", b: string | boolean, c: number | undefined } How can I make a generic that derives such collapsed unions? I need a generic that supports unions of any size. Similar

why does explicit cast for generic list not work

拥有回忆 提交于 2021-02-20 00:48:38
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

why does explicit cast for generic list not work

巧了我就是萌 提交于 2021-02-20 00:48:00
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

why does explicit cast for generic list not work

半世苍凉 提交于 2021-02-20 00:44:33
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

How to define an aggregated ICollection<T> where T is type of the current declaring class within a hierarchy?

拜拜、爱过 提交于 2021-02-19 12:56:19
问题 I need to inherit a collection of items of the current type, like this class A { // some properties... public ICollection<A> Children; } class B: A { // other properties } This mostly works as expected. The problem is I can do something like this class C: A { } B b = new B(); b.Children = new List<C>(); Is there any way to force b.Children to be a collection of B ? 回答1: No, there is no way to do such thing yet. The C# language has no artifact to declare such thing: class A { public

How to define an aggregated ICollection<T> where T is type of the current declaring class within a hierarchy?

纵饮孤独 提交于 2021-02-19 12:51:46
问题 I need to inherit a collection of items of the current type, like this class A { // some properties... public ICollection<A> Children; } class B: A { // other properties } This mostly works as expected. The problem is I can do something like this class C: A { } B b = new B(); b.Children = new List<C>(); Is there any way to force b.Children to be a collection of B ? 回答1: No, there is no way to do such thing yet. The C# language has no artifact to declare such thing: class A { public

How to define an aggregated ICollection<T> where T is type of the current declaring class within a hierarchy?

别说谁变了你拦得住时间么 提交于 2021-02-19 12:48:31
问题 I need to inherit a collection of items of the current type, like this class A { // some properties... public ICollection<A> Children; } class B: A { // other properties } This mostly works as expected. The problem is I can do something like this class C: A { } B b = new B(); b.Children = new List<C>(); Is there any way to force b.Children to be a collection of B ? 回答1: No, there is no way to do such thing yet. The C# language has no artifact to declare such thing: class A { public

Java generic method not working - parameter not being restricted [duplicate]

こ雲淡風輕ζ 提交于 2021-02-19 08:01:47
问题 This question already has answers here : Why does this generic java method accept two objects of different type? (4 answers) Closed 2 years ago . Say I have this code: class Demo { static <T> T pick(T a1, T a2) { return a2; } public static void main(String[] args) { pick("d", 123); } } From what I learned, it seems like I have stated that the two parameters a1 , a2 and the return type of pick must be under the same generic type T . So why is the compiler allowing me to pass a String and an

How to set type parameter bound in scala to make generic function for numerics?

非 Y 不嫁゛ 提交于 2021-02-19 07:12:58
问题 I want to make a sum function that works with all Numeric types. This works: object session { def mapReduce[A](f: A => A, combine: (A, A) => A, zero: A, inc: A) (a: A,b: A) (implicit num:Numeric[A]): A = { def loop(acc: A, a: A) = if (num.gt(a, b)) acc else combine(f(a), mapReduce(f, combine, zero, inc)(num.plus(a, inc), b)) loop(zero, a) } def sum(f: Int => Int) (a: Int, b: Int) : Int = { mapReduce(f, (x: Int, y: Int) => x + y, 0, 1)(a, b)} sum(x => x)(3, 4) //> res0: Int = 7 def product(f: