A relaxed chat room about all things Scala. Beginner questions welcome. https://scala-lang.org/conduct/ applies
SethTisue on 2.13.x
2.12: new Scala SHA (#1399) unfork specs2 Merge commit '4bafba0925491ab34… and 1 more (compare)
SethTisue on 2.12.x
unfork specs2 (compare)
I wasted a day struggling with this:
trait Model {
type State
}
trait Algorithm {
val model: Model
type State = model.State
def infer: State
}
val m: Model = new Model {
type State = Int
}
val a = new Algorithm {
val model = m
def infer = 1
}
This throws an error:
ScalaFiddle.scala:21: error: type mismatch;
found : scala.this.Int(1)
required: $anon.this.State
(which expands to) $anon.this.model.State
def infer = 1
^
I stared at the longest time on the definition of val a
because that's where the error was coming from, but eventually realized the cause was my definition of val m
. If I remove the : Model
and make it val m = new Model {...}
it works.
: Model
you remove the refinement
m: Model {type State = Int }
Int
later
List[Algorithm]
Algorithm[State]
you woudn't be able to put them in a List with a proper type
trait MyTrait[T]
you're saying MyTrait[T]
exists for every T
but if you say trait MyTrait {type T}
you're making no such guarantees
it sort of makes sense... coz when you define
trait MyTrait[T]
you're sayingMyTrait[T]
exists for everyT
but if you saytrait MyTrait {type T}
you're making no such guarantees
yes, you are making a different guarantee, that it exists a T for which MyTrait
exist