path-dependent-type

Scala types: Class A is not equal to the T where T is: type T = A

杀马特。学长 韩版系。学妹 提交于 2019-12-19 19:17:11
问题 I was reading the section 20.7 of the book Programming in Scala and I was wondering why while this code compiles: class Food class Fish extends Food class Grass extends Food abstract class Animal { type SuitableFood <: Food def eat(food: SuitableFood) } class Cow extends Animal { type SuitableFood = Grass override def eat(food: Grass) {} } val bessy: Animal = new Cow bessy eat (new bessy.SuitableFood) This code does not (the rest of the code is the same as before, only the last line changes):

What is meant by Scala's path-dependent types?

谁说胖子不能爱 提交于 2019-12-17 02:55:07
问题 I've heard that Scala has path-dependent types. It's something to do with inner-classes but what does this actually mean and why do I care? 回答1: My favorite example: case class Board(length: Int, height: Int) { case class Coordinate(x: Int, y: Int) { require(0 <= x && x < length && 0 <= y && y < height) } val occupied = scala.collection.mutable.Set[Coordinate]() } val b1 = Board(20, 20) val b2 = Board(30, 30) val c1 = b1.Coordinate(15, 15) val c2 = b2.Coordinate(25, 25) b1.occupied += c1 b2

Scala: Re-use generic resulting from path-dependent type in path-dependent context

浪尽此生 提交于 2019-12-12 15:23:39
问题 In short: The following does not compile (reason below), how can I make it work? trait Simulator { type CM[T] def useCM(v: CM[_]) } case class CMH[S <: Simulator,T](cm: S#CM[T]) class SimIterator[S <: Simulator](val sim: S, val cmhs: Seq[CMH[S,_]]) { cmhs foreach { cmh => sim.useCM(cmh.cm) } /* compile time error: type mismatch; found : cmh.cm.type (with underlying type S#CM[_$2]) required: SimIterator.this.sim.CM[_] Note: _$2 <: Any (and cmh.cm.type <: S#CM[_$2]), but type CM is invariant in

Scala: immutability and path-dependent type compatibility

喜欢而已 提交于 2019-12-12 11:34:33
问题 I have asked a few questions around this topic but this time I want to make it a more general discussion, since it seems to me that Scala is lacking some very important blocks. Consider the following code (which is simplified from my real project), trait World { type State <: StateIntf def evolve(s: State): State def initialState: State } class Algorithm(world: World) { def process(s: world.State) { val s1 = world.evolve(s) // ... do something with s and s1 } } Everything seems so beautiful

Referring to a the sub-type of a path-dependent type

爷,独闯天下 提交于 2019-12-11 11:20:53
问题 The following works: class Outter { type Inner = Either[Int,String] type L = Left[Int,String] type R = Right[Int,String] def f(x: Inner) = 1 } val o = new Outter o.f(new o.L(1)) o.f(new o.R("name")) but only because there is an explicit type member for all the sub-types of the Inner . Is it possible to construct a value from a sub-type of a path-dependent type without the need to mention them explicitly in the Outter ? Like: class Outter { type Inner = Either[Int,String] def f(x: Inner) = 1 }

Overriding members having Path Dependent types in Scala. Explanation is needed in terms of Scala Language Specification

ε祈祈猫儿з 提交于 2019-12-11 06:23:03
问题 Consider the following simple Scala experiment: scala> trait A {class C;val c:C} defined trait A scala> object O1 extends A {val c=new C} defined object O1 scala> object O2 extends A {val c=O1.c} <console>:9: error: overriding value c in trait A of type O2.C; value c has incompatible type object O2 extends A {val c=O1.c} According to the Scala Language Specification (SLS 5.1.4): The type of the value O1.c should conform to the type of A.c because the former overrides the latter. Question 1:

abstract type in scala

时光总嘲笑我的痴心妄想 提交于 2019-12-10 09:34:55
问题 I am just going through abstract type in Scala and I got an error The example I was trying: scala> class Food abstract class Animal { type SuitableFood <: Food def eat(food: SuitableFood) } defined class Food defined class Animal scala> class Grass extends Food class Cow extends Animal { type SuitableFood = Grass override def eat(food: Grass) {} } defined class Grass defined class Cow scala> class Fish extends Food defined class Fish scala> val bessy: Animal = new Cow bessy: Animal = Cow

Preventing unwanted combinations using path-dependent types?

左心房为你撑大大i 提交于 2019-12-08 05:57:22
问题 Major edit to better reflect my intent: // this *must* be outside of Team class Player class Team(val p1: Player, val p2: Player) { type P = ??? // <-- what to put here ? see below ... // perhaps I can somehow construct a union type: // type P = p1.type union p2.type // or perhaps the following : // type P = Either[p1.type,p2.type] // The purpose of `P` here is to define a type which can only hold p1 or p2 for values. def position(p: P) = p match { case `p1` => 1 case `p2` => 2 // Gotcha ! we

Is a Path Dependent Type a subtype?

人盡茶涼 提交于 2019-12-07 16:19:48
问题 trait A { trait B { def foo: A.this.B = new B{} def bar: A#B = foo def baz: A.this.B = bar // type mismatch; found : A#B required: A.this.B } } Am I right that A.this.B is a path dependent type?! (That's my understanding so far) Does the example above mean that the type A.this.B is a subtype of A#B ? (If yes, I guess the difference is that an instance of A.this.B has a reference to the instance of A compared to A#B which doesn't?) Does anyone know an enlightening explanation that resolves my

Simulate a path-dependent type in Haskell

China☆狼群 提交于 2019-12-07 13:54:33
问题 Here is a simplified example of what I want to do. Let's say you have a HList of pairs: let hlist = HCons (1, "1") (HCons ("0", 2) (HCons ("0", 1.5) HNil)) Now I want to write a function replaceAll which will replace all the "keys" of a given type with the first "value" of the same type. For example, with the HList above I would like to replace all the String keys with "1" which is the first value of type String found in the HList replaceAll @String hlist = HCons (1, "1") (HCons ("1", 2)