0
我想在Scala中實現Phantom Reference來替換finalize()。我有一個文件對象,需要使用Phantom Reference進行GC化。雖然在java中有一些代碼示例,但我無法在Scala中找到任何東西。我曾嘗試在斯卡拉寫這樣的:如何在Scala中使用PhantomReference
val q = new ReferenceQueue()
val phantom = new PhantomReference(file,q)
但我收到以下錯誤
found : java.lang.ref.ReferenceQueue[Nothing]
[error] required: java.lang.ref.ReferenceQueue[_ >: java.io.File]
[error] Note: Nothing <: Any, but Java-defined class ReferenceQueue is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error] val phantom = new PhantomReference(file,q)
我明白我失去了一些東西微不足道,但我不是在Scala中很精通。有人可以幫忙嗎?
非常感謝。這工作。我無法相信我錯過了這一點。 – G3M
這仍然不回答原來的問題,這是如何在Scala中實現PhantomReference – G3M