Re: Statements before super()
On Tue, 9 Feb 2010, Lew wrote:
Pitch wrote:
Also, you cannot place any code before super(...) which I already new,
and it also sucks. :)
[...]
Anyone knows why is that so?
Eric Sosman wrote:
[...] If it were possible to
run arbitrary code on a Sub instance before its Super-ness had
been established and its Super invariants put in place, you'd
be working with a Sub that was a Super in name only, but not in
actuality.
Steven Simpson wrote:
Could that restriction not be loosened in a compiler-verifiable way,
i.e. check that no statement prior to super() uses 'this' (explicitly or
implicitly)? Therefore, there would be no arbitrary code acting on the
Sub instance, until after super().
Anything is possible, but it isn't going to happen.
Which is a shame.
In the thread you abandoned to post this, Steven, there was a code
example of why it isn't necessary.
No, there was an example constructed to show that it wasn't necessary, but
which actually showed that it wasn't necessary in one particular case.
Try:
/** An ordered pair of strings. */
class StringPair {
public StringPair(String first, String second) {...}
}
/** Particular kind of string pair which is formed by splitting a string
at the first instance of a given separator character. For example:
new SeparatedString("foo=bar", '=')
is equivalent to:
new StringPair("foo", "bar)
*/
class SeparatedString {
public SeparatedString(String str, char separator) {
// now what?
}
}
Unless you want the split the string twice, your only option is recourse
to a static factory method.
Allowing expressions that didn't touch 'this' before the super call would
have been easy, safe, and much more useful. In terms of safety, it's no
different to allowing expressions as the parameters to super calls, but
much more useful.
tom
--
Hit to death in the future head