[Ikvm-developers] RE: Ghosts and java.lang.CharSequence
Brought to you by:
jfrijters
|
From: Jonathan P. <jp...@ny...> - 2003-07-31 16:51:07
|
Jeroen,
>I finally looked at your implementation. You are trading one evil
>for another one ;-) You will have object identity problems
I don't think the identity issue is a problem since the spec doesn't require
it.
According the the JDK 1.4.2 docs:
This interface does not refine the general contracts of the equals and
hashCode methods. The result of comparing two objects that implement
CharSequence is therefore, in general, undefined. Each object may be
implemented by a different class, and there is no guarantee that each class
will be capable of testing its instances for equality with those of the
other. It is therefore inappropriate to use arbitrary CharSequence instances
as elements in a set or as keys in a map.
>I think the code generation will be harder than for my solution.
You'll need to make this decision.
>How would you compile this:
>
>Object o = "foo";
>CharSequence seq = (CharSequence)o;
>
>Presumably like this...
>
>Object o = "foo";
>CharSequence seq;
>if(o instanceof String)
> seq = new StringWrapper((String)o);
>else
> seq = (CharSequence)o;
This is exactly what I was thinking.
Now what happens when I test identity:
if(seq == "foo")
{
// ...
}
This fails, unless seq is unwrapped again. Or are you suggestion always
wrapping strings in a StringWrapper? This has it's own set of problems:
- you lose object identity when interoperating with other .NET code
- performance loss due to extra indirection (this is significant for
strings)
- need to write own version of String.intern()
- it works for String (because it is final), but it is not a general
solution (doesn't work for making Throwable appear to implement
Serializable)
Regards,
Jeroen
|