- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why does scala.ref.ReferenceQueue.remove() return Option[...]?
Wed, 2011-06-15, 02:16
Is there a reason why scala.ref.ReferenceQueue.remove() returns
Option[Reference[T]] instead of Reference[T]? I ask because although it
makes sense for remove(long) to have return type Option[...] since it
returns None after the timeout, it does not make sense for remove() to
have return type Option[...] because it always succeeds --- unless it
has been interrupted, in which case InterruptedException is thrown and
None is *still* not returned. Thus, it seems to me that
scala.ref.ReferenceQueue.remove() really ought to just return
Reference[T] directly rather than always adding a redundant Some()
wrapper around its output.
If I am correct in inferring that this was just an oversight, then
perhaps could we change scala.ref.ReferenceQueue in some way to address
this --- either by changing the return type of remove(), or by adding a
new method?
Cheers,
Greg
Wed, 2011-06-15, 02:47
#2
Re: Why does scala.ref.ReferenceQueue.remove() return Option[..
On 6/14/11 6:25 PM, Tony Morris wrote:
> On 15/06/11 11:16, Gregory Crosswhite wrote:
>> Is there a reason why scala.ref.ReferenceQueue.remove() returns
>> Option[Reference[T]] instead of Reference[T]? I ask because although
>> it makes sense for remove(long) to have return type Option[...] since
>> it returns None after the timeout, it does not make sense for remove()
>> to have return type Option[...] because it always succeeds --- unless
>> it has been interrupted, in which case InterruptedException is thrown
>> and None is *still* not returned. Thus, it seems to me that
>> scala.ref.ReferenceQueue.remove() really ought to just return
>> Reference[T] directly rather than always adding a redundant Some()
>> wrapper around its output.
>>
>> If I am correct in inferring that this was just an oversight, then
>> perhaps could we change scala.ref.ReferenceQueue in some way to
>> address this --- either by changing the return type of remove(), or by
>> adding a new method?
>>
>> Cheers,
>> Greg
> It returns None when the null value is pulled off the queue.
>
Yes, I understand that it *would* do that if
java.lang.ref.ReferenceQueue.remove() ever returned null, but according
to the documentation it does not return null:
http://download.oracle.com/javase/6/docs/api/java/lang/ref/ReferenceQueue.html#remove()
So unless there is some sort of weird undocumented behavior here (which
I acknowledge is certainly possible and would explain why this approach
was taken), it would seem that it is not possible for a null value to be
pulled off the queue and so remove() will *always* return Some(...).
Cheers,
Greg
On 15/06/11 11:16, Gregory Crosswhite wrote:
> Is there a reason why scala.ref.ReferenceQueue.remove() returns
> Option[Reference[T]] instead of Reference[T]? I ask because although
> it makes sense for remove(long) to have return type Option[...] since
> it returns None after the timeout, it does not make sense for remove()
> to have return type Option[...] because it always succeeds --- unless
> it has been interrupted, in which case InterruptedException is thrown
> and None is *still* not returned. Thus, it seems to me that
> scala.ref.ReferenceQueue.remove() really ought to just return
> Reference[T] directly rather than always adding a redundant Some()
> wrapper around its output.
>
> If I am correct in inferring that this was just an oversight, then
> perhaps could we change scala.ref.ReferenceQueue in some way to
> address this --- either by changing the return type of remove(), or by
> adding a new method?
>
> Cheers,
> Greg
It returns None when the null value is pulled off the queue.