Menu

#168 Missing compiler errors

open
nobody
None
5
2005-10-16
2005-10-16
Anonymous
No

The following is accepted by the compiler (using
Eclipse plugin v0.9.8). Note the illegal (I think)
implementing of non-abstract interfaces, and the fact
that an interface can implment an abstract interface:

abstract interface AbInterface1 {
(int, int) getDimensions();

int getArea() {
(int width, int length) = this.getDimensions();
return width * length;
}
}

interface Interface1 implements AbInterface1 {}
interface Interface2 extends AbInterface1 {}

class java.lang.String implements Interface1;
class java.lang.String implements Interface2;
getDimensions(String s) = (1, s.length());

<AbInterface1 T> void printout1(T x) {
println(x.getArea());
}

void printout2(Interface1 x) {
println(x.getArea());
}

void printout3(Interface2 x) {
println(x.getArea());
}

void main(String[] args) {
String s = "Hello World!";
print("Printout 1: ");
printout1(s);

try {
print("Printout 2: ");
printout2(s);
}catch(Exception e) {
e.printStackTrace();
}

try {
print("Printout 2 - Interface1: ");
Interface1 x = s;
printout2(x);
}catch(Exception e) {
e.printStackTrace();
}

try {
print("Printout 3: ");
printout3(s);
}catch(Exception e) {
e.printStackTrace();
}

try {
print("Printout 3 - Interface2: ");
Interface2 x = s;
printout3(x);
}catch(Exception e) {
e.printStackTrace();
}
}

The output produced when run is:

Printout 1: 12
Printout 2: Argument #0 to 'printout2' has wrong type
(java.lang.String)
at gnu.mapping.WrongType.make(WrongType.java:56)
at temp.fun.main(fun.nice:8)
at temp.dispatch.main(dispatch.nice)
Caused by: java.lang.ClassCastException: java.lang.String
... 2 more
Printout 2 - Interface1: java.lang.ClassCastException:
java.lang.String
at temp.fun.main(fun.nice:15)
at temp.dispatch.main(dispatch.nice)
Printout 3: Argument #0 to 'printout3' has wrong type
(java.lang.String)
at gnu.mapping.WrongType.make(WrongType.java:56)
at temp.fun.main(fun.nice:23)
at temp.dispatch.main(dispatch.nice)
Caused by: java.lang.ClassCastException: java.lang.String
... 2 more
Printout 3 - Interface2: java.lang.ClassCastException:
java.lang.String
at temp.fun.main(fun.nice:30)
at temp.dispatch.main(dispatch.nice)

Discussion


Log in to post a comment.

MongoDB Logo MongoDB