path-dependent-type

Scala shapeless KList with extra constraint

帅比萌擦擦* 提交于 2019-12-06 07:01:43
问题 I want to take this pattern: def accept[T](a: RList[T]) = true def accept[T, V](a: RList[T], b: RList[V])(implicit ev: a.S =:= b.S) = true def accept[T, V, Q](a: RList[T], b: RList[V], c: RList[Q])(implicit ev: a.S =:= b.S, ev2: b.S =:= c.S) = true but have it accept a KList , instead of manually overriding for all arities. Basically I want to say, "Take any number of RList s, that have the same S member type" RList is a trait, that contains a type S . (For more background on an RList and why

Exposing a path-dependent type coming from a singleton type

放肆的年华 提交于 2019-12-06 02:40:55
问题 I'm trying to make Scala find the right type for a path-dependent type coming from a singleton type. First, here is the type container for the example, and one instance: trait Container { type X def get(): X } val container = new Container { type X = String def get(): X = "" } I can see the String in this first attempt (so I already have a working scenario): class WithTypeParam[C <: Container](val c: C) { def getFromContainer(): c.X = c.get() } val withTypeParam = new WithTypeParam[container

Is a Path Dependent Type a subtype?

半世苍凉 提交于 2019-12-06 00:38:14
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 confusion with these two types? The excellent book Programming in Scala has a pretty good explanation :

Simulate a path-dependent type in Haskell

我怕爱的太早我们不能终老 提交于 2019-12-05 22:32:38
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) (HCons ("1", 1.5) HNil)) This seems to require path-dependent types in order to "extract" the type of the

abstract type in scala

烈酒焚心 提交于 2019-12-05 17:21:35
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@5c404da8 scala> bessy.eat(new bessy.SuitableFood) <console>:13: error: class type required but bessy

Constrain function based on origin (Path Dependent type? Type Generation?)

一世执手 提交于 2019-12-04 20:54:54
问题 Sorry about terrible title, not sure of a better one. Here's a gross simplification of my problem (Sorry if it seems so trivial, that it's pointless): class RList[T](data: List[T]) { def map[V](f: T=>V): RList[V] = ... } The idea of an RList (Restricted List) is that you can't resize it, or change the order of the elements in it. But you can use functions that give you a new RList with changed data in it. Now there needs to be a function that creates RLists. It might have a signature

Scala shapeless KList with extra constraint

混江龙づ霸主 提交于 2019-12-04 14:40:42
I want to take this pattern: def accept[T](a: RList[T]) = true def accept[T, V](a: RList[T], b: RList[V])(implicit ev: a.S =:= b.S) = true def accept[T, V, Q](a: RList[T], b: RList[V], c: RList[Q])(implicit ev: a.S =:= b.S, ev2: b.S =:= c.S) = true but have it accept a KList , instead of manually overriding for all arities. Basically I want to say, "Take any number of RList s, that have the same S member type" RList is a trait, that contains a type S . (For more background on an RList and why I'm doing this, see: Constrain function based on origin (Path Dependent type? Type Generation?) ) It

Path-Dependent type inside class value in Scala

只愿长相守 提交于 2019-12-04 13:54:07
问题 I would like to give a value of a type with an abstract type to a class and later use it's path dependent type. Look at the following example (using Scala 2.10.1): trait Foo { type A def makeA: A def useA(a: A): Unit } object Test { class IntFoo extends Foo { type A = Int def makeA = 1 def useA(a: Int) = println(a) } class FooWrap(val a: Foo) { def wrapUse(v: a.A) = a.useA(v) } val foo = new IntFoo /* Path dependent locally */ val bar = foo bar.useA(foo.makeA) // works /* Path dependent

Exposing a path-dependent type coming from a singleton type

别等时光非礼了梦想. 提交于 2019-12-04 08:51:13
I'm trying to make Scala find the right type for a path-dependent type coming from a singleton type. First, here is the type container for the example, and one instance: trait Container { type X def get(): X } val container = new Container { type X = String def get(): X = "" } I can see the String in this first attempt (so I already have a working scenario): class WithTypeParam[C <: Container](val c: C) { def getFromContainer(): c.X = c.get() } val withTypeParam = new WithTypeParam[container.type](container) // good, I see the String! val foo: String = withTypeParam.getFromContainer() But when

Constrain function based on origin (Path Dependent type? Type Generation?)

时光怂恿深爱的人放手 提交于 2019-12-03 13:53:54
Sorry about terrible title, not sure of a better one. Here's a gross simplification of my problem (Sorry if it seems so trivial, that it's pointless): class RList[T](data: List[T]) { def map[V](f: T=>V): RList[V] = ... } The idea of an RList (Restricted List) is that you can't resize it, or change the order of the elements in it. But you can use functions that give you a new RList with changed data in it. Now there needs to be a function that creates RLists. It might have a signature something like: def toRList[T](values: List[T]): RList[T] = ... So far, so good. But now the tricky part. I