return in void functions

bearophile bearophileHUGS at lycos.com
Wed Mar 4 13:56:52 PST 2009


D has safeties to avoid this kind of code, because it's essentially considered a bug:

int foo() {}
void main() {
    foo();
}


But this compiles with no errors:

void foo() {
    return 1;
}
void main() {
    foo();
}


Notice that this Java code:

class Test {
	static void foo() {
		return 1;
	}

    public static void main(String[] args) {
		Test.foo();
    }
}

Raises a compilation error:

Test.java:3: cannot return a value from method whose result type is void
                return 1;
                       ^
1 error

I think this avoids some mistakes.

Bye,
bearophile



More information about the Digitalmars-d mailing list