path-dependent-type

When calling a scala function with compile-time macro, how to failover smoothly when it causes compilation errors?

一世执手 提交于 2021-02-13 05:43:30
问题 Assuming that I intend to use the singleton/literal type feature in a scala program, this feature is provided in shapeless library in scala 2.12 (scala 2.13 supports native literal type but let's use shapeless as an example) In shapeless, literal type is represented as a path-dependent inner type of Witness object, which can be implicitly converted from a scala literal/const: import com.tribbloids.spike.BaseSpec import shapeless.Witness import scala.util.Random val w: Witness.Lt[Int] = 3 val

Enforcing that dependent return type must implement typeclass

吃可爱长大的小学妹 提交于 2020-11-29 10:15:13
问题 I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec typeclass below, they must also ensure that the return value of the getElem method implements another typeclass ( IsVecElem ). My attempts to make this work look something like this: // A typeclass for an vector element abstract class IsVecElem[A, T: Numeric] { def dataOnly(self: A): T } // A typeclass for a vector abstract class IsVec[A, T:

Enforcing that dependent return type must implement typeclass

巧了我就是萌 提交于 2020-11-29 10:13:43
问题 I am trying to enforce a rule that the (dependent) return type of a typeclass, must itself implement a typeclass. So when the user implements the IsVec typeclass below, they must also ensure that the return value of the getElem method implements another typeclass ( IsVecElem ). My attempts to make this work look something like this: // A typeclass for an vector element abstract class IsVecElem[A, T: Numeric] { def dataOnly(self: A): T } // A typeclass for a vector abstract class IsVec[A, T:

How to create an instances for typeclass with dependent type using shapeless

旧城冷巷雨未停 提交于 2020-11-29 09:32:58
问题 I'm trying to derive a tuple instance for a type class with dependent type. I'm using shapeless to create summon the type class for the tuple elements. I'm having trouble matching tuple instance types: import shapeless.the import simulacrum.typeclass @typeclass trait Identifiable[M] { type K def identify(id: M): K } object Identifiable{ implicit def identifiableTuple[K1: Identifiable, K2: Identifiable]: Identifiable[(K1,K2)] = new Identifiable[(K1,K2)]{ val b = the[Identifiable[K2]] val a =

What is the difference between path-dependent types and dependent types?

允我心安 提交于 2020-05-12 11:50:13
问题 Scala has path-dependent types, but it is said that Scala doesn’t support dependent typing. What is the difference between path-dependent types and dependent types? As far as I understand, path-dependent types are one kind of dependent types. 回答1: A dependent type is a type that depends on a value. A path dependent type is a specific kind of dependent type in which the type depends on a path. I am not sure if the term "path dependent type" exists outside the Scala community. In any case, the

Path-dependent argument type is not enforced (?)

杀马特。学长 韩版系。学妹 提交于 2020-01-06 15:48:10
问题 class Outter { type Inner = Either[Int, String] def f(x: this.Inner) = 1 } val o = new Outter val someLeft = Left(1) o.f(someLeft) tried at the REPL: scala> :load Learn.scala Loading Learn.scala... defined class Outter o: Outter = Outter@28037ced someLeft: scala.util.Left[Int,Nothing] = Left(1) res0: Int = 1 Which confuses the hell out of me as Outter.f() has an explicit path-dependent type argument ( x: this.Inner ) which means only those Left s (or Right s) that are (a sub-type of) o.Inner

Returning a path-dependent type

江枫思渺然 提交于 2020-01-03 17:11:32
问题 How can I design a method which returns a path dependent type? In the following example, I deliberately want Vertex to be path dependent on Tree such that it is forbidden to mix vertices across trees (and this is just an example): trait Tree { trait Vertex def root: Vertex def addChild(parent: Vertex): Vertex } trait TreeFactory { def make: Tree } Now the following cannot be constructed: def test(f: TreeFactory): (Tree, Map[Tree#Vertex, Tree#Vertex]) = { val t = f.make var sq = IndexedSeq(t

Path Dependent Types example doesn't work

牧云@^-^@ 提交于 2019-12-25 07:25:52
问题 Scala n00b here. Pretty sure I understand PDT's but was making sure and hit a problem; here's a previous question Scala types: Class A is not equal to the T where T is: type T = A with sample code which I'll reproduce here: 1: class Food 2: class Fish extends Food 3: class Grass extends Food 4: 5: abstract class Animal { 6: type SuitableFood <: Food 7: def eat(food: SuitableFood) 8: } 9: 10: class Cow extends Animal { 11: type SuitableFood = Grass 12: override def eat(food: Grass) {} 13: } 14

Correlate two type parameters

試著忘記壹切 提交于 2019-12-24 10:27:50
问题 Suppose, I have a sequence of operations, some of which depend on some of the results of previous ones. Something like this: type Results = List[(Operation[_], Any)] // ??? trait Operation[Out] { type Result = Out def apply(results: Results): Out } class SomeOp extends Operation[String] { def apply(results: Results) = "foo" } class OtherOp extends Operation[String] { def apply(results: Results) = results .collectFirst { case (_: SomeOp, x: String) => x } .getOrElse("") + "bar" } def applyAll(

Map with path-dependent value type?

江枫思渺然 提交于 2019-12-22 04:34:08
问题 I know that Scala has path-dependent types, so for example if I have a class within an inner class I can constrain one argument of a method to be an instance of the inner class of the other argument: class Outer { class Inner( x:Int ) } val o1 = new Outer val o2 = new Outer val i11 = new o1.Inner(11) val i12 = new o1.Inner(12) val i21 = new o2.Inner(21) def f[ A <: Outer ]( a:A )( i:a.Inner ) = (a,i) f(o1)(i11) // works f(o1)(i21) // type mismatch; i21 is from o2, not o1 And I can create a