generics

Clarifying contravariance nature of the return type of a function as parameter a function of an outer convariant container

﹥>﹥吖頭↗ 提交于 2021-01-01 09:40:48
问题 In Option we have def getOrElse[B >: A](default: => B): B = this match { case None => default case Some(a) => a } def orElse[B >: A](obj: => Option[B]): Option[B] = this match { case None => obj case _ => this } In Either we have: def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] I understand what is going and why, a rather extended example could be this OrElse( { Option[B]}).map{....} If B is such that A :> B, then if Some(a) you get Some(a).map(f:B => ???) then Kaboom generally

Kotlin - how to return “self type” in a subclass? (without an extension function)

☆樱花仙子☆ 提交于 2021-01-01 04:19:15
问题 Let's have these classes: class A { fun foo(): A = this } class B: A() { fun bar() { ... } } Now I would like Kotlin to detect when I call foo on B , and give me the result typed as B . So that I can write: B().foo().bar() With kotlin 1.4.20, this is not possible - it would need an explicit cast to B after foo() . Perhaps it could be handled by the compiler, if it clearly sees that the function returns this . Or maybe we could hint it explicitly... class A { fun <Self> foo(): Self = this } I

Kotlin - how to return “self type” in a subclass? (without an extension function)

拜拜、爱过 提交于 2021-01-01 04:18:22
问题 Let's have these classes: class A { fun foo(): A = this } class B: A() { fun bar() { ... } } Now I would like Kotlin to detect when I call foo on B , and give me the result typed as B . So that I can write: B().foo().bar() With kotlin 1.4.20, this is not possible - it would need an explicit cast to B after foo() . Perhaps it could be handled by the compiler, if it clearly sees that the function returns this . Or maybe we could hint it explicitly... class A { fun <Self> foo(): Self = this } I

Operator '+' cannot be applied to types 'T' and 'T'. in TypeScript

前提是你 提交于 2021-01-01 02:41:06
问题 I am very new to TypeScript. My TypeScript version is 3.7.5. IMHO, it should be very easy, but I don't know why it does not work. function add<T> (a:T, b:T):T { return a + b ; } console.log(add (5, 6)); I get the error: Operator '+' cannot be applied to types 'T' and 'T'. I used this one also: function add<T extends string | number > (a:T, b:T):T The same error is there. If I can not use + for this generic, why should I use generics ? 回答1: Generics are not the right approach here. You cannot

Generics with Spock Stub

谁都会走 提交于 2020-12-31 04:43:39
问题 I cannot make compile Spock stub for generic class. The signature of constructor is following: SomeClass(SerSup<Cap> capSup, String foo, String bar); I need to stub the first argument. The following are my failed attempts. First try: def someClass = new SomeClass(Stub(SerSup<Cap>), "foo", "bar") Error: Groovyc: unexpected token: > Status bar: ',' or ')' expected Another try: def someClass = new someClass(Stub(Cup) as SerSup<Cup>, "foo" ,"bar") groovy.lang.MissingMethodException: No signature

Generics with Spock Stub

故事扮演 提交于 2020-12-31 04:41:45
问题 I cannot make compile Spock stub for generic class. The signature of constructor is following: SomeClass(SerSup<Cap> capSup, String foo, String bar); I need to stub the first argument. The following are my failed attempts. First try: def someClass = new SomeClass(Stub(SerSup<Cap>), "foo", "bar") Error: Groovyc: unexpected token: > Status bar: ',' or ')' expected Another try: def someClass = new someClass(Stub(Cup) as SerSup<Cup>, "foo" ,"bar") groovy.lang.MissingMethodException: No signature

Generics with Spock Stub

一世执手 提交于 2020-12-31 04:41:19
问题 I cannot make compile Spock stub for generic class. The signature of constructor is following: SomeClass(SerSup<Cap> capSup, String foo, String bar); I need to stub the first argument. The following are my failed attempts. First try: def someClass = new SomeClass(Stub(SerSup<Cap>), "foo", "bar") Error: Groovyc: unexpected token: > Status bar: ',' or ')' expected Another try: def someClass = new someClass(Stub(Cup) as SerSup<Cup>, "foo" ,"bar") groovy.lang.MissingMethodException: No signature

TypeScript infer the callback return type in type constructor

人盡茶涼 提交于 2020-12-30 03:42:46
问题 I want to write a type constructor for a function which receives a type S and a function from S to another type then applies that function on the S and returns the result: // This works but it's tied to the implementation function dig<S, R>(s: S, fn: (s: S) => R): R { return fn(s); } // This works as separate type constructor but I have to specify `R` type Dig<S, R> = (s: S, fn: (s: S) => R) => R; // Generic type 'Dig' requires 2 type argument(s). const d: Dig<string> = (s, fn) => fn(s); So

TypeScript infer the callback return type in type constructor

家住魔仙堡 提交于 2020-12-30 03:41:25
问题 I want to write a type constructor for a function which receives a type S and a function from S to another type then applies that function on the S and returns the result: // This works but it's tied to the implementation function dig<S, R>(s: S, fn: (s: S) => R): R { return fn(s); } // This works as separate type constructor but I have to specify `R` type Dig<S, R> = (s: S, fn: (s: S) => R) => R; // Generic type 'Dig' requires 2 type argument(s). const d: Dig<string> = (s, fn) => fn(s); So

Typed Generic Key Value Interface in Typescript

混江龙づ霸主 提交于 2020-12-29 06:09:10
问题 I have the following example Object: let foo: Foo = { 'key1': { default: 'foo', fn: (val:string) => val }, 'key2': { default: 42, fn: (val:number) => val }, // this should throw an error, because type of default and fn don't match 'key3': { default: true, fn: (val:string) => val } } The Interface should look something like this: interface Foo { [key: string]: { default: T, fn: (val:T) => any } } This of course doesn't work, because there's no T defined. So I thought about doing this: