2

Dotty は、型パラメーターを持つクラスを型メンバーを持つクラスに desugar すると報告されています。次に例を示します。

class C[T, U] { }
// <=>
class C {
  type C$T
  type C$U
}

次の例のように、Dotty はポリモーフィック メソッドをどのように desugar しますか?

def m[T, U](x: T, u: U): T = x
// <=>
?
4

2 に答える 2

5

ポリモーフィック クラスとは異なり、ポリモーフィックメソッドは脱糖されません。それらは基本的にポリモーフィックなままです。

于 2016-08-26T17:25:21.303 に答える
2

Nada Amin、Samuel Grütter、Martin Odersky、Tiark Rompf、および Sandro Stucki による最新の DOT 論文The Essence of DOT の13 ページの下部、セクション 5.2 から始めることをお勧めします。List[+A]これは、 Scalaでの単純な共変ポリモーフィック型の実装を示しています。特に注目すべきは、ポリモーフィックcons[A]メソッドです。

package scala.collection.immutable

trait List[+A] {
  def isEmpty: Boolean
  def head: A
  def tail: List[A]
}

object List {
  def cons[A](hd: A, tl: List[A]) = new List[A] {
    def isEmpty = false
    def head = hd
    def tail = tl
  }
}

そして、それがどのように DOT にエンコードされているか:

let scala_collection_immutable = ν(sci) {
  List = μ(self: {A; isEmpty: bool.Boolean; head: self.A; tail: sci.List∧{A <: self.A}})

cons: ∀(x: {A})∀(hd: x.A)∀(tl: sci.List∧{A <: x.A})sci.List∧{A <: x.A} =
  λ(x: {A})λ(hd: x.A)λ(tl: sci.List∧{A <: x.A})
    let result = ν(self) {
      A = x.A; isEmpty = bool.false; head = hd; tail = tl }
    in result
}: { μ(sci: {
  List <: μ(self: {A; head: self.A; tail: sci.List∧{A <: self.A}})

  cons: ∀(x: {A})∀(hd: x.A)∀(tl: sci.List∧{A <: x.A})sci.List∧{A <: x.A}
})}
in …

これにより、Dotty でどのようにエンコードされているかが直感的にわかるはずです。

15 ページでは、脱糖された DOT を Scala にマッピングする方法を示します。

object scala_collection_immutable { sci => 
  trait List { self =>
    type A
    def isEmpty: Boolean
    def head: self.A
    def tail: List{type A <: self.A}
  }

  def cons(x: {type A})(hd: x.A)(tl: sci.List{type A <: x.A}) 
    : sci.List{type A <: x.A} = new List{ self =>
    type A = x.A
    def isEmpty = false
    def head = hd
    def tail = tl
  }
}

お分かりのように、ポリモーフィック メソッドのエンコーディングはポリモーフィック トレイトと多かれ少なかれ同じです。型パラメーターは抽象型のメンバーになります。この場合は、洗練された型 (別名、構造型) の抽象型のメンバーです。

x : A
// becomes
x : {type A}
于 2016-08-26T20:40:05.913 に答える