> On 04-Oct-1999, George Russell <[EMAIL PROTECTED]> wrote:
> > (b) a type of compile-time if statement which will branch on simple
> > arithmetic and boolean expressions in constant values and constants,
> > which doesn't typecheck the branch which isn't taken.  For example,
> > this is what Java provides and I think it's not too bad.
> 
> Are you sure Java provides that?

It doesn't. There's no special treatment of constant conditionals, except
that clever (or rather a not totally braindamaged) compiler may be
expected to optimize the unreachable branch away. At least this is what
several Java books say.

Example (the two classes must be in two separate files):

public class Features {
    public static final boolean hasStringMultiply = false;
}

public class CondTest {
    public static int foo(int bar) {
        if(Features.hasStringMultiply) {
            return bar * "Fortytwo";
        } else {
            return bar * 42;
        }
    }

    public static void main(String[] args) {
        System.out.println(foo(42));
    }
}


This doesn't compile (fortunately). I don't think that compilers that skip
type checking (or even syntax checking) for unreachable code.

Kili

-- 
de: Signaturen erzeugen Krebs.
en: Signatures cause cancer.
Please send other translations.




Reply via email to