type-bounds

Scala: overriding type member with bounds

爱⌒轻易说出口 提交于 2019-12-24 03:25:51
问题 I've narrowed down my issue to the following minimal (non-)working example: class Z trait A[E <: Z] { type T[X <: E] <: A[X] } trait B[E <: Z] extends A[E] { type T[X <: E] <: B[X] } trait C[E <: Z, F[X <: Z] <: C[X, F]] extends A[E] { type T[X <: E] = F[X] } class D[E <: Z] extends B[E] with C[E, D] It is sort of a higher-kinded F-bounded polymorphism (I'm using T[E] as the return value of some methods). When I try to compile this code I get: error: overriding type T in trait B with bounds[X

How to set lower-bound to type parameters in TypeScript?

偶尔善良 提交于 2019-12-23 19:08:07
问题 In TypeScript (my version: 2.1.1), it is okay to set an upper-bound to type parameters like this: class Animal {} class Cat extends Animal {} class Kitten extends Cat{} function foo<A extends Animal>(a: A) { /* */ } How can I set a lower-bound on A ? The following code does not work: class Animal {} class Cat extends Animal {} class Kitten extends Cat{} function foo<A super Kitten>(a: A) { /* */ } It will cause a compilation error. People in the GitHub issue #13337 of the TypeScript repo have

How does the type inferencer work on reduceLeft?

我们两清 提交于 2019-12-22 10:14:31
问题 Further to my other question about reduceLeft , the signature of reduceLeft on Seq is def reduceLeft [B >: A] (f: (B, A) ⇒ B): B and we can call it with expressions such as List(1,2,3,4) reduceLeft (_ + _) In this example A is Int , so reduceLeft expects a Function2[B >: Int, Int, B] . Regardless of how reduceLeft works (which is irrelevant), how does the type inferencer know that B has a + method, when it could be of type Any ? 回答1: I think the section 6.26.4 Local Type Inference of the spec

Why doesn't type inference work here?

独自空忆成欢 提交于 2019-12-22 05:22:01
问题 This problem arose in a module I'm writing, but I have made a minimal case that exhibits the same behaviour. class Minimal[T](x : T) { def doSomething = x } object Sugar { type S[T] = { def doSomething : T } def apply[T, X <: S[T]] (x: X) = x.doSomething } object Error { val a = new Minimal(4) Sugar(a) // error: inferred [Nothing, Minimal[Int]] does not fit the bounds of apply Sugar[Int, Minimal[Int]](a) // works as expected } The problem is that the compiler manages to figure out the inner

Why doesn't type inference work here?

╄→гoц情女王★ 提交于 2019-12-22 05:21:39
问题 This problem arose in a module I'm writing, but I have made a minimal case that exhibits the same behaviour. class Minimal[T](x : T) { def doSomething = x } object Sugar { type S[T] = { def doSomething : T } def apply[T, X <: S[T]] (x: X) = x.doSomething } object Error { val a = new Minimal(4) Sugar(a) // error: inferred [Nothing, Minimal[Int]] does not fit the bounds of apply Sugar[Int, Minimal[Int]](a) // works as expected } The problem is that the compiler manages to figure out the inner

Is there special meaning to an underscore (_) in Type Bounds?

左心房为你撑大大i 提交于 2019-12-21 02:22:11
问题 I'm trying to understand Scala's existential types. Is there any difference between: def foo[X <: Bar] = 3 and def foo[_ <: Bar] = 3 or are they something more than just unnamed type parameters? 回答1: Here _ is indeed just an unnamed type parameter, no more, no less. There is no difference between def foo[_ <: Bar] = 3 and def foo[X <: Bar] = 3 where X is unused. UPDATE : In response to: "I can't think of a use case for an unused type, I'd be grateful for one": Note that this is pretty much

Cannot override a type with non-volatile upper bound

﹥>﹥吖頭↗ 提交于 2019-12-20 20:12:06
问题 I have a compiler error in scala and I don't know what does it refer to: Assume these declarations: trait Abstract { type MyType } trait AInner trait A extends Abstract{ type MyType <: AInner } trait BInner { def bMethod : Int } trait B extends Abstract with A{ override type MyType <: BInner with A#MyType } What I'm trying to achieve here(in trait B ) is to further restrict the type MyType declared in Abstract , so any value of type MyType must extend all the MyType s in the mixin tree. The

Types in Scala - lower bounds

回眸只為那壹抹淺笑 提交于 2019-12-12 01:25:58
问题 on code below. My expectation is that T must be a of type B or A , so call to lowerBound(new D) should probably not compile (?). Similar experiments with upperbound give me expected typecheck errors. Thanks for giving the hint. object varianceCheck { class A { override def toString = this.getClass.getCanonicalName } class B extends A class C extends B class D extends C def lowerBound[T >: B](param: T) = { param } println(lowerBound(new D)) //> varianceCheck.D } 回答1: With your implementation

Upper and Lower bound on scala type

不羁的心 提交于 2019-12-10 15:50:35
问题 Consider the following hierarchy: class C1 class C2 extends C1 class C3 extends C2 class C4 extends C3 I want to write a function that just accepts types C2 and C3 . For that I thought of the following: def f [C >: C3 <: C2](c :C) = 0 I'd expect the following behaviour f(new C1) //doesn't compile, ok f(new C2) //compiles, ok f(new C3) //compiles, ok f(new C4) // !!! Compiles, and it shouldn't The problem is when calling it with C4 , which I don't want to allow, but the compiler accepts. I

How does the type inferencer work on reduceLeft?

Deadly 提交于 2019-12-05 19:22:50
Further to my other question about reduceLeft , the signature of reduceLeft on Seq is def reduceLeft [B >: A] (f: (B, A) ⇒ B): B and we can call it with expressions such as List(1,2,3,4) reduceLeft (_ + _) In this example A is Int , so reduceLeft expects a Function2[B >: Int, Int, B] . Regardless of how reduceLeft works (which is irrelevant), how does the type inferencer know that B has a + method, when it could be of type Any ? I think the section 6.26.4 Local Type Inference of the spec sort of explains what's going on. The compiler will look for an optimal type. When the type parameter is