Questions tagged [path-dependent-type]
152 questions
5
votes
2 answers
Path-dependent types without a path?
Consider this trivial example:
class Outer {
case class Inner()
def test(i: Inner) = {}
}
As expected, this doesn't compile because of a type mismatch:
val o1 = new Outer()
val o2 = new Outer()
o1.test(o2.Inner()) // doesn't compile
What if…
![](../../users/profiles/775806.webp)
n. 'pronouns' m.
- 95,181
- 13
- 111
- 206
5
votes
3 answers
Scala types: Class A is not equal to the T where T is: type T = A
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:…
![](../../users/profiles/2252717.webp)
vicaba
- 2,676
- 1
- 22
- 40
5
votes
1 answer
scalaz.Equal for path dependent types
I'm experimenting with path-dependent types and I run into a problem when trying to write a scalaz.Equal instance for it. I have the following structure:
class A {
class B
}
val a1 = new A
val b1 = new a1.B // type a1.B
val a2 = new A
val…
![](../../users/profiles/652378.webp)
Chirlo
- 5,679
- 1
- 25
- 42
5
votes
1 answer
Return type depending on parameter type (e.g. Await.ready()) does not work as expected
Scala Await.ready() method is defined like this:
def ready[T](awaitable: Awaitable[T], atMost: Duration): awaitable.type = ...
That is, the return type is a path-dependent type of awaitable parameter. I thought that (and IntelliJ IDEA agreed with…
![](../../users/profiles/788207.webp)
Vladimir Matveev
- 97,409
- 27
- 241
- 264
5
votes
1 answer
abstract type in scala
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…
![](../../users/profiles/1490530.webp)
Rahul Kulhari
- 1,045
- 1
- 13
- 42
5
votes
1 answer
How to get the classtag of a path-dependent type
I have an abstract path-dependent type that I need to ClassTag of. Is there a better way than manually pulling the implicit for each concrete derived class?
trait Foo {
type A // : ClassTag // Need the ClassTag of A later
val ctA: ClassTag[A] //…
![](../../users/profiles/323177.webp)
Woodz
- 846
- 5
- 21
5
votes
1 answer
Avoid type cast with type member and sub-typing
I am having trouble with path dependent types and pattern matching:
trait View[A]
trait Foo {
type Bar
def defaultBar: Bar
}
trait Baz extends Foo {
def view(init: Bar): View[Bar]
}
trait Test {
val foo: Foo
def bar: foo.Bar =…
![](../../users/profiles/515054.webp)
0__
- 64,257
- 16
- 158
- 253
5
votes
2 answers
Scala - Lower bound inference in path-dependent types
I'm trying to understand why can't the Scala compiler infer the following restriction on a path-dependent type:
trait MyTrait
class MyTraitImpl extends MyTrait
trait MyTrait2[A <: MyTrait] {
type MyTraitType = A
}
class MyTrait2Impl[A <: MyTrait]…
![](../../users/profiles/603293.webp)
Rui Gonçalves
- 1,335
- 12
- 25
5
votes
1 answer
Constrain a class with implicit evidence
Say I have this:
trait Animal {
type Species
}
I can easily enough write a function that only takes two animals of the same species
def breed(a: Animal, b: Animal)(implicit evidence: a.Species =:= b.Species) = ???
but I want to create a…
![](../../users/profiles/377956.webp)
Heptic
- 2,806
- 4
- 26
- 48
5
votes
2 answers
How to avoid awful type casts working with path dependent types?
I am new to Scala and dont know why i have to do an (unintuitive for me) type cast related to path dependent types in the following code.
(I don't like getters, setters nor nulls, they are here to separate operations and disambiguate the source of…
![](../../users/profiles/1815253.webp)
Guillermo Pollitzer
- 53
- 5
5
votes
1 answer
How can I override a method with a dependent return type?
I'm having trouble in Scala 2.9.2 implementing a method which declares a dependent return type. The following code
object DependentTypesQuestion {
def ??? = throw new UnsupportedOperationException
trait X {
trait Y
}
trait Z {
def…
![](../../users/profiles/82970.webp)
Scott Morrison
- 3,090
- 21
- 39
5
votes
1 answer
Scala: Equivalence of path-dependent types
How do I get around with equivalence of two path-dependent types that I know are the same but the compiler does not?
Using Scala 2.10.0 M7 I am trying to convert an AST from one universe to another.
case class MacroBridge(context: Context) {
def…
![](../../users/profiles/3827.webp)
Eugene Yokota
- 90,473
- 43
- 204
- 301
4
votes
1 answer
Can Scala notice identities between path-dependent types?
Sometimes in Scala I find that I get type mismatches related to path-dependent types, but I can easily reason that in fact the types coincide. Here's a simple example:
trait Foo { trait Bar }
object Main extends App {
val foo1 = new Foo { }
val…
![](../../users/profiles/82970.webp)
Scott Morrison
- 3,090
- 21
- 39
4
votes
3 answers
Returning a path-dependent type
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 {
…
![](../../users/profiles/515054.webp)
0__
- 64,257
- 16
- 158
- 253
4
votes
1 answer
How do existential types overlap with path-dependent types?
Starting Scala 3 existential types have been dropped and one of the reasons is stated as
Existential types largely overlap with path-dependent types, so the
gain of having them is relatively minor.
Given "largely", so not always, I was wondering…
![](../../users/profiles/5205022.webp)
Mario Galic
- 41,266
- 6
- 39
- 76