Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.87 KB | None | 0 0
  1. //
  2.  
  3. // Hlist = HNil | Cons(T, HList)
  4.  
  5. sealed trait HList {
  6.   def ::[H](h: H): HCons[H, this.type] = HCons(h, this)  // красненькое - баг idea
  7. }
  8. object HList {
  9.   type ::[+H, +T <: HList] = HCons[H, T]
  10.   type HNil = HNil.type
  11.  
  12.   trait Appendable[L <: HList, R <: HList, Res <: HList] {
  13.     def apply(l: L, r: R): Res
  14.   }
  15.   object Appendable {
  16.     //Res == L append R
  17.     //H :: Res == (H :: L) append R
  18.     //       < == >
  19.     //Res == L append R
  20.     //База: HNil == HNil append R
  21.     implicit def base[R <: HList]: Appendable[HNil, R, R] = new Appendable[HNil, R, R] {
  22.       override def apply(l: HNil, r: R): R = r
  23.     }
  24.     implicit def step[H, L <: HList, R <: HList, Res <: HList]
  25.                      (implicit appendable: Appendable[L, R, Res])
  26.                      : Appendable[H :: L, R, H :: Res] =
  27.       new Appendable[H :: L, R, H :: Res] {
  28.         override def apply(l: H :: L, r: R): H :: Res = {
  29.           l.head :: appendable.apply(l.tail, r)
  30.         }
  31.       }
  32.   }
  33.  
  34.   def append[L <: HList, R <: HList, Res <: HList]
  35.   (l: L, r: R)(implicit appendable: Appendable[L, R, Res]): Res = {
  36.     appendable(l, r)
  37.   }
  38. }
  39.  
  40. case object HNil extends HList
  41. case class HCons[+H, +T <: HList](head: H, tail: T) extends HList {
  42.   //
  43. }
  44.  
  45. import HList._
  46. val l: String :: Int :: HNil = "text" :: 1 :: HNil
  47. val r: String :: Boolean :: HNil = "text2" :: false :: HNil
  48.  
  49. append(l, r)
  50.  
  51.  
  52. // множественное наследование
  53.  
  54. trait A1 {
  55.   def foo: Int
  56. }
  57. trait B1 extends A1 {
  58.   override def foo: Int = 1
  59. }
  60. trait C1 extends A1 {
  61.   override def foo: Int = 2
  62. }
  63. class D1 extends B1 with C1 {}
  64.  
  65. new D1().foo
  66.  
  67. // линеаризация
  68. // D1 C1 A1 B1 A1
  69.  
  70. // удалили дубликаты (есть зависимость B1 -> A1), поэтому удалили первую, а не вторую
  71. // D1 C1 B1 A1
  72.  
  73. trait A
  74. trait B extends A
  75. trait C
  76. trait D extends A
  77. trait E extends D with C
  78. trait F extends B
  79. class G extends E with F
  80.  
  81. // G [F B A] [E [C] [D A]]
  82.  
  83. //
  84.  
  85. case class AB(x: Int, y : Int)
  86. AB(1, 2).copy(y = 3)
  87.  
  88. //
  89.  
  90. // bounds
  91.  
  92. class A2[T <: String]
  93. def f11[T <: String](t: T): T = t
  94. f11(2)
  95.  
  96. //co- contra- variance
  97. // co = +, contra = -
  98. class Boo[+T]
  99. class Animal
  100. class Duck extends Animal
  101. class Hare extends Animal
  102.  
  103. val b7: Boo[Animal] = new Boo[Duck]
  104. class Goo[-T]
  105. val g7: Goo[Duck] = new Goo[Animal]
  106.  
  107. // пример для (-T): передача функции в функцию, в ней можно ограничить тип на меньший / больший
  108.  
  109. class Boo2[+T](t: T) {
  110.   def foo[B >: T](t: B): T = ???
  111.   def bar(t: Goo[T]): T = ???
  112. }
  113. class Animal2
  114. class Duck2 extends Animal2
  115. class Hare2 extends Animal2
  116. class Goo2[-T <: HList](t: T) {
  117.   def foo(x: T) = 1
  118. }
  119.  
  120. val b8: Boo2[Animal2] = new Boo2[Duck2](new Duck2)
  121. val g8: Goo2[Duck2] = new Goo2[Animal2](new Hare2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement