次のようなジェネリック クラスを作成しようとしています。
class A[T](v: Option[T]) {
def this(v: T) = this(Some(v))
def this() = this(None)
def getV = v
}
次に、いくつかのテストを行います。
scala> new A getV
res21: Option[Nothing] = None
scala> new A(8) getV
res22: Option[Int] = Some(8)
ここまでは順調ですね。しかし、メイン コンストラクターを呼び出そうとするとすぐに、次のようになります。
scala> new A(Some(8)) getV
<console>:9: error: ambiguous reference to overloaded definition,
both constructor A in class A of type (v: T)A[T]
and constructor A in class A of type (v: Option[T])A[T]
match argument types (Some[Int])
new A(Some(8)) getV
^
scala> new A(None) getV
<console>:9: error: ambiguous reference to overloaded definition,
both constructor A in class A of type (v: T)A[T]
and constructor A in class A of type (v: Option[T])A[T]
match argument types (None.type)
new A(None) getV
^
これら 2 つのコンストラクターの間で「あいまい」な点は何ですか? それとも (推測させてください) Scala の型システムについて私が知らないことはもう 1 つありますか? :)
確かに、非ジェネリック クラスを使用すると、すべてが期待どおりに機能します。私B
のクラスは問題なく動作します:
class B(v: Option[String]) {
def this(v: String) = this(Some(v))
def this() = this(None)
def getV = v
}
scala> new B("ABC") getV
res26: Option[String] = Some(ABC)
scala> new B getV
res27: Option[String] = None
scala> new B(Some("ABC")) getV
res28: Option[String] = Some(ABC)
scala> new B(None) getV
res29: Option[String] = None