Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Hlist = HNil | Cons(T, HList)
- sealed trait HList {
- def ::[H](h: H): HCons[H, this.type] = HCons(h, this) // красненькое - баг idea
- }
- object HList {
- type ::[+H, +T <: HList] = HCons[H, T]
- type HNil = HNil.type
- trait Appendable[L <: HList, R <: HList, Res <: HList] {
- def apply(l: L, r: R): Res
- }
- object Appendable {
- //Res == L append R
- //H :: Res == (H :: L) append R
- // < == >
- //Res == L append R
- //База: HNil == HNil append R
- implicit def base[R <: HList]: Appendable[HNil, R, R] = new Appendable[HNil, R, R] {
- override def apply(l: HNil, r: R): R = r
- }
- implicit def step[H, L <: HList, R <: HList, Res <: HList]
- (implicit appendable: Appendable[L, R, Res])
- : Appendable[H :: L, R, H :: Res] =
- new Appendable[H :: L, R, H :: Res] {
- override def apply(l: H :: L, r: R): H :: Res = {
- l.head :: appendable.apply(l.tail, r)
- }
- }
- }
- def append[L <: HList, R <: HList, Res <: HList]
- (l: L, r: R)(implicit appendable: Appendable[L, R, Res]): Res = {
- appendable(l, r)
- }
- }
- case object HNil extends HList
- case class HCons[+H, +T <: HList](head: H, tail: T) extends HList {
- //
- }
- import HList._
- val l: String :: Int :: HNil = "text" :: 1 :: HNil
- val r: String :: Boolean :: HNil = "text2" :: false :: HNil
- append(l, r)
- // множественное наследование
- trait A1 {
- def foo: Int
- }
- trait B1 extends A1 {
- override def foo: Int = 1
- }
- trait C1 extends A1 {
- override def foo: Int = 2
- }
- class D1 extends B1 with C1 {}
- new D1().foo
- // линеаризация
- // D1 C1 A1 B1 A1
- // удалили дубликаты (есть зависимость B1 -> A1), поэтому удалили первую, а не вторую
- // D1 C1 B1 A1
- trait A
- trait B extends A
- trait C
- trait D extends A
- trait E extends D with C
- trait F extends B
- class G extends E with F
- // G [F B A] [E [C] [D A]]
- //
- case class AB(x: Int, y : Int)
- AB(1, 2).copy(y = 3)
- //
- // bounds
- class A2[T <: String]
- def f11[T <: String](t: T): T = t
- f11(2)
- //co- contra- variance
- // co = +, contra = -
- class Boo[+T]
- class Animal
- class Duck extends Animal
- class Hare extends Animal
- val b7: Boo[Animal] = new Boo[Duck]
- class Goo[-T]
- val g7: Goo[Duck] = new Goo[Animal]
- // пример для (-T): передача функции в функцию, в ней можно ограничить тип на меньший / больший
- class Boo2[+T](t: T) {
- def foo[B >: T](t: B): T = ???
- def bar(t: Goo[T]): T = ???
- }
- class Animal2
- class Duck2 extends Animal2
- class Hare2 extends Animal2
- class Goo2[-T <: HList](t: T) {
- def foo(x: T) = 1
- }
- val b8: Boo2[Animal2] = new Boo2[Duck2](new Duck2)
- val g8: Goo2[Duck2] = new Goo2[Animal2](new Hare2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement