Re: Alternatives languages on the JVM: which one or is there no alternative?
On 26.11.2013 22:28, Volker Borchert wrote:
No language that is to run on the JVM can overcome its fundamental
shortcoming of missing multiple implementation inheritance.
I have come to believe that omitting things that other languages do was
actually a wise decision of the language designers. Avoiding MI might
be one of them. Granted, you cannot use implementation inheritance.
But sometimes not making these things easy is actually an advantage. At
least I think that when comparing C++ and Java. The equation might
calculate differently when comparing Java to other languages (Eiffel?).
While
this might not be a problem with everyday code, is _is_ a problem
if you are in low-level or framework stuff, for example if you have
class hierarchies where you want to offer mixed reference flavors -
your class C either ends up with two fields, one T and one to some
(X extends WeakReference<T>) which contains another extra field for
the back reference to the containing C so that the C can be notified
of the X having been garbage collected and an extra two-word object
header, or with lots of duplicated code.
What's wrong with this?
class Foo<T> {
private Object ref;
protected T getItem() {
return ref instanceof WekReference<?> ?
((WeakReference<T>) ref).get() :
(T) ref;
}
public T doSomeWork() {
final T x = getItem();
// work ...
return x;
}
}
Then you need a pair of setters or one setter and one method which
switches reference mode. This avoids having two fields and it avoids
"lots of duplicated code".
I do not know your notification requirements but I am sure you are aware
of ReferenceQueue. Do you need notification the very moment an object
is collected? How do you do that? The only way I can think of is
having an additional thread blocking in ReferenceQueue.remove(). Not
sure though why I then would place a field in the WeakReference -
wouldn't it make more sense to let T know of its container / owner?
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/