Edit-
:
,
"" , . Foo . :
sealed trait Foo[M[_], P]
case class Bar[M[_]]() extends Foo[M, Int]
def f[M[_], S](x: Foo[M, S]): Int = x match {
case Bar() => g[M]
}
def g[M[_]] = 0
2.12.4 :
error: M takes no type parameters, expected: one
case Bar() => g[M]
^
one error found
- P of Foo, Int Bar, S f.
, . , , , , 2.11, 2.12.
, , .
1. :
sealed trait Foo[M[_], P]
case class Bar[M[_]]() extends Foo[M, Int]
def f[M[_], S](x: Foo[M, S]): Int = x match {
case b: Bar[M] => g[M]
}
def g[M[_]] = 0
2.
(, Scala match - case!), , [Odersky et al. " Scala", , . 275]:
().
, , m:
sealed trait Foo[M[_], P]
case class Bar[M[_]]() extends Foo[M, Int]
def f[M[_], S](x: Foo[M, S]): Int = x match {
case b: Bar[M] => g[M]
}
def g[M[_]] = 0
}
3. RHS case Bar:
sealed trait Foo[M[_], P]
case class Bar[M[_]]() extends Foo[M, Int] {
def g: Int = 0
}
def f[M[_], S](x: Foo[M, S]): Int = x match {
case b @ Bar() => b.g
}
4. ( , )
, P Int , :
sealed trait Foo[M[_], P]
case class Bar[M[_]]() extends Foo[M, Int]
def f[M[_]](x: Foo[M, Int]): Int = x match {
case Bar() => g[M]
}
def g[M[_]] = 0
, :
1. :
import scala.language.higherKinds
sealed trait Wrapping
sealed trait PinkWrap extends Wrapping
sealed trait GreenWrap extends Wrapping
sealed trait Foo[M[_], A] {}
case class MintFoo[M[_], A](a : A) extends Foo[M, A]
case class LiquoriceFoo[M[_], A](a : A) extends Foo[M, A]
sealed trait WrappedFoo[M[_], _, A]
case class FooInPinkWrap[M[_], A](m: Foo[M, A]) extends WrappedFoo[M, PinkWrap, A]
case class FooInGreenWrap[M[_], A](m: Foo[M, A]) extends WrappedFoo[M, GreenWrap, A]
object Utils {
def analyzeFoo[M[_], S <: Wrapping, A](w: WrappedFoo[M, S, A]): String = {
w match {
case f: FooInPinkWrap[M, A] => tasteFoo[M, A](f.m) + " in Pink wrapping"
case f: FooInGreenWrap[M, A] => tasteFoo[M, A](f.m) + " in Green wrapping"
}
}
def tasteFoo[M[_], A](f: Foo[M,A]) : String = {
f match {
case MintFoo(a) => "Mint"
case LiquoriceFoo(a) => "Liquorice"
}
}
}
2. :
sealed trait Wrapping
sealed trait PinkWrap extends Wrapping
sealed trait GreenWrap extends Wrapping
sealed trait Foo[M[_], A] {}
case class MintFoo[M[_], A](a : A) extends Foo[M, A]
case class LiquoriceFoo[M[_], A](a : A) extends Foo[M, A]
sealed trait WrappedFoo[M[_], _, A]
case class FooInPinkWrap[M[_], A](m: Foo[M, A]) extends WrappedFoo[M, PinkWrap, A]
case class FooInGreenWrap[M[_], A](m: Foo[M, A]) extends WrappedFoo[M, GreenWrap, A]
object Utils {
def analyzeFoo[M[_], S <: Wrapping, A](w: WrappedFoo[M, S, A]): String = {
w match {
case f: FooInPinkWrap[m, a] => tasteFoo[m, a](f.m) + " in Pink wrapping"
case f: FooInGreenWrap[m, a] => tasteFoo[m, a](f.m) + " in Green wrapping"
}
}
def tasteFoo[M[_], A](f: Foo[M,A]) : String = {
f match {
case MintFoo(a) => "Mint"
case LiquoriceFoo(a) => "Liquorice"
}
}
}
3: tasteBlah WrappedFoo:
sealed trait Wrapping
sealed trait PinkWrap extends Wrapping
sealed trait GreenWrap extends Wrapping
sealed trait Foo[M[_], A] {}
case class MintFoo[M[_], A](a : A) extends Foo[M, A]
case class LiquoriceFoo[M[_], A](a : A) extends Foo[M, A]
sealed trait WrappedFoo[M[_], _, A] {
def m: Foo[M, A]
def tasteFoo: String = {
m match {
case MintFoo(a) => "Mint"
case LiquoriceFoo(a) => "Liquorice"
}
}
}
case class FooInPinkWrap[M[_], A](m: Foo[M, A]) extends WrappedFoo[M, PinkWrap, A]
case class FooInGreenWrap[M[_], A](m: Foo[M, A]) extends WrappedFoo[M, GreenWrap, A]
object Utils {
def analyzeFoo[M[_], S <: Wrapping, A](w: WrappedFoo[M, S, A]): String = {
w match {
case f: FooInPinkWrap[M, A] => f.tasteFoo + " in Pink wrapping"
case f: FooInGreenWrap[M, A] => f.tasteFoo + " in Green wrapping"
}
}
}
4 ( , ).
( ): , , . - , , .