- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Type-Parameterized Method
Sat, 2009-03-21, 02:45
Hi,
I'm writing a cache (using WeakHashMap) and wrote this method to capture
the repeated pattern of checking for an element within the cache and
returning that instance instead of the instance used as a probe or
query. There a few kinds of FOL_Term and I'm trying to cache them all
in the same WeakHashMap. When I try the parameterized form of the
method declaration, I get the compiler diagnostic shown below:
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
import scala.collection.jcl.WeakHashMap
// ...
private
def
cacheTerm[T <: FOL_Term](term: T): T =
// cacheTerm(term: FOL_Term): FOL_Term =
{
if (termCache.contains(term))
termCache.get(term).get // <-- Line 173
else {
termCache.put(term, term)
term
}
}
// ...
private
val
termCache: WeakHashMap[FOL_Term, FOL_Term] = new WeakHashMap()
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
/dar/rho/src/rho/cdt/CachingBuilder.scala
Error:Error:line (173)error: type mismatch;
found : adt.this.FOL_Term
required: T&0
termCache.get(term).get
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
If I use the commented-out form of the method declaration, I have to
cast the result of cacheTerm(...) to the required FOL_Term subtype,
which is always the same as that of the "term" argument.
Can I do what I'm trying? If so, how?
Randall Schulz
Sat, 2009-03-21, 06:17
#2
Re: Type-Parameterized Method
Besides the typing, it also demonstrates another error: one should
never check weak hashes for containment and then assume that the value
still exists in the next statement.
On Fri, Mar 20, 2009 at 9:02 PM, Vlad Patryshev wrote:
> I guess the problem is that T is in both covariant and contravariant
> position here. Which makes it kind of hard to deal with.
>
> 2009/3/20 Randall R Schulz
>>
>> Hi,
>>
>> I'm writing a cache (using WeakHashMap) and wrote this method to capture
>> the repeated pattern of checking for an element within the cache and
>> returning that instance instead of the instance used as a probe or
>> query. There a few kinds of FOL_Term and I'm trying to cache them all
>> in the same WeakHashMap. When I try the parameterized form of the
>> method declaration, I get the compiler diagnostic shown below:
>>
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>> import scala.collection.jcl.WeakHashMap
>>
>> // ...
>>
>> private
>> def
>> cacheTerm[T <: FOL_Term](term: T): T =
>> // cacheTerm(term: FOL_Term): FOL_Term =
>> {
>> if (termCache.contains(term))
>> termCache.get(term).get // <-- Line 173
>> else {
>> termCache.put(term, term)
>> term
>> }
>> }
>>
>> // ...
>>
>> private
>> val
>> termCache: WeakHashMap[FOL_Term, FOL_Term] = new WeakHashMap()
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>>
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>> Information:Compilation completed with 1 error and 0 warnings
>> Information:1 error
>> Information:0 warnings
>> /dar/rho/src/rho/cdt/CachingBuilder.scala
>> Error:Error:line (173)error: type mismatch;
>> found : adt.this.FOL_Term
>> required: T&0
>> termCache.get(term).get
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>>
>>
>> If I use the commented-out form of the method declaration, I have to
>> cast the result of cacheTerm(...) to the required FOL_Term subtype,
>> which is always the same as that of the "term" argument.
>>
>>
>> Can I do what I'm trying? If so, how?
>>
>>
>> Randall Schulz
>
>
>
> --
> Thanks,
> -Vlad
>
Sat, 2009-03-21, 08:27
#3
Re: Type-Parameterized Method
But you can use soft references for this. It's hard to get the GC to GC soft
references.
Also, Google Collections is very useful for creating maps with weak, soft and
strong keys and/or values in maps. The only thing you can't do is use soft
references with value equality instead of reference equality with them to build
an interner, but see:
http://groups.google.com/group/google-collections-users/browse_thread/th...
http://groups.google.com/group/google-collections-users/browse_thread/th...
Regards,
Blair
Sat, 2009-03-21, 14:07
#4
Re: Type-Parameterized Method
On Friday March 20 2009, Christian Szegedy wrote:
> Besides the typing, it also demonstrates another error: one should
> never check weak hashes for containment and then assume that the
> value still exists in the next statement.
Indeed. I'll deal with that separately.
Can the type issue be solved?
Randall Schulz
Sat, 2009-03-21, 16:47
#5
Re: Type-Parameterized Method
>
> Can the type issue be solved?
Yes, make termCache object private (private[this]). This should solve the issue.
See my recent blog about function Memoization [1] which talks a bit about this.
Michael
Sat, 2009-03-21, 16:57
#6
Re: Type-Parameterized Method
On Saturday March 21 2009, Randall R Schulz wrote:
> On Friday March 20 2009, Christian Szegedy wrote:
> > Besides the typing, it also demonstrates another error: one should
> > never check weak hashes for containment and then assume that the
> > value still exists in the next statement.
>
> Indeed. I'll deal with that separately.
Looking at the WeakHashMap, I see that it can't be used to cache objects
by associating the cached value with itself, since the value side of
the association is a strong reference that prevents the entry from
being reclaimed when no other references exist.
The cache system is not critical right now, I'm going to move on to
other areas that are more important.
Randall Schulz
Sat, 2009-03-21, 17:07
#7
Re: Re: Type-Parameterized Method
On Saturday March 21 2009, Michael wrote:
> > Can the type issue be solved?
>
> Yes, make termCache object private (private[this]).
I don't think I understand that.
Given the current declaration:
private
val
termCache: WeakHashMap[FOL_Term, FOL_Term] = new WeakHashMap()
... how would it look to reflect your suggestion?
> This should solve the issue. See my recent blog about function
> Memoization [1] which talks a bit about this.
>
> Michael
>
> [1] http://michid.wordpress.com/2009/02/23/function_mem/
Thanks. I'm actively (desperately?) seeking information on Scala
programming. (I keep vacillating between thinking I'm getting it and
feeling bewildered.)
Randall Schulz
2009/3/20 Randall R Schulz <rschulz@sonic.net>
--
Thanks,
-Vlad