trait Foo {
type Arg
type Prod
def makeProd(a: Arg): Prod
}
object Test {
def f1(x: Foo)(y: x.Arg) = x.makeProd(y)
case class f2(x: Foo) {
def apply(y: x.Arg) = x.makeProd(y)
}
val myFoo = new Foo {
type Arg = Int
type Prod = (Int, Int)
def makeProd(i: Int) = (i, i)
}
def main(args: Array[String]): Unit = {
println(f1(myFoo)(5)) // works
println(f2(myFoo)(10)) // nope
// test/files/run/t102.scala:22: error: type mismatch;
// found : Int(10)
// required: _1.x.Arg where val _1: Test.f2
// println(f2(myFoo)(10))
// ^
// one error found
}
}
How about,
case class f2[T <: Foo](x: T) {
def apply(y: x.Arg) = x.makeProd(y)
}
Cheers,
Miles
--
Miles Sabin
tel: +44 7813 944 528
gtalk: mi...@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin