Khi suy ra các loại, trình biên dịch thường cần tính toán Giới hạn dưới cùng (LUB) của một danh sách các loại. Ví dụ: loại if (cond) e1 else e1
là LUB của các loại e1
và e1
.
Những loại có thể nhận được khá lớn, ví dụ thử điều này trong một REPL:
:type Map(1 -> (1 to 10), 2 -> (1 to 10).toList)
scala.collection.immutable.Map[Int,scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int] with Serializable{def reverse: scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]{def reverse: scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def dropRight(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def takeRight(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def drop(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def take(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]}; def dropRight(n: Int): scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]{def reverse: scala.collection.immutable.Seq[Int] with scala.collection.AbstractSeq[Int]; def dropRight(n: Int): scala.collection.immutable.Seq[Int]...
này commit giới thiệu một số sự tỉnh táo kiểm tra để hạn chế độ sâu của loại suy ra như vậy.
Đã có một số công việc gần đây cho plugin trong quá trình biên dịch để phát hiện loại suy luận mất nhiều thời gian để tính toán và đề xuất những nơi chú thích loại rõ ràng có thể thận trọng.
[blog này] (http://pchiusano.blogspot.com/2011/05/making-most-of-scalas-extremely-limited.html) có một số thảo luận thú vị về chủ đề này – Jamil
Có, nó đề cập đến một loại loại scala sẽ từ chối suy luận: một hàm tạo kiểu được áp dụng một phần. Tôi tự hỏi nếu có những người khác. – Owen
Tôi khuyên bạn nên đăng lên danh sách gửi thư bằng ngôn ngữ scala được đề cập ở đây: http://www.scala-lang.org/node/199 – david