Names of static types

S

Stefan Ram

I have noticed a difference in report style between the
following error messages:

interface A {}
interface A_ {}
class B implements A, A_ {}
class C implements A, A_ {}

public class Main
{ static void f( final java.lang.String s ){}
public static void main( java.lang.String[] args )
{ f( Math.random() < 0.5 ? new B() : new C() ); }}

Main.java:49: f(java.lang.String) in Main cannot be applied to (java.lang.Object&A&A_)
{ f( Math.random() < 0.5 ? new B() : new C() ); }}
^
Here, one can notice that "java.lang.Object" is being
mentioned. If "A_" is removed, one might expect
"java.lang.Object&A" as type, but gets only "A":

interface A {}
class B implements A {}
class C implements A {}

public class Main
{ static void f( final java.lang.String s ){}
public static void main( java.lang.String[] args )
{ f( Math.random() < 0.5 ? new B() : new C() ); }}

Main.java:45: f(java.lang.String) in Main cannot be applied to (A)
{ f( Math.random() < 0.5 ? new B() : new C() ); }}
^
Why is "java.lang.Object" dropped now? Or, why was it
mentioned in the first case?

And why can I not declare variables of such "&"-types?

interface A {}
interface B {}

public class Main
{ static void f( final java.lang.String s ){}
public static void main( java.lang.String[] args )
{ A&B a; }}

How are these static types (in parentheses and with "&")
called, which are mentioned in error message, but can not be
used in the source code?

OK, if this was needed, I could write

interface C implements A, B {} (...) C a;

(The compiler used was from a recent mustang (1.6) build.)
 
T

Thomas Hawtin

Stefan said:
I have noticed a difference in report style between the
following error messages:

interface A {}
interface A_ {}
class B implements A, A_ {}
class C implements A, A_ {}

public class Main
{ static void f( final java.lang.String s ){}
public static void main( java.lang.String[] args )
{ f( Math.random() < 0.5 ? new B() : new C() ); }}

Main.java:49: f(java.lang.String) in Main cannot be applied to (java.lang.Object&A&A_)
{ f( Math.random() < 0.5 ? new B() : new C() ); }}
^
Here, one can notice that "java.lang.Object" is being
mentioned. If "A_" is removed, one might expect
"java.lang.Object&A" as type, but gets only "A":

Presumably so as not to give prominence to one interface over another.
The first type mentioned in an intersection type is the only one that
can be a class and is used for erasure.
interface A {}
class B implements A {}
class C implements A {}

public class Main
{ static void f( final java.lang.String s ){}
public static void main( java.lang.String[] args )
{ f( Math.random() < 0.5 ? new B() : new C() ); }}

Main.java:45: f(java.lang.String) in Main cannot be applied to (A)
{ f( Math.random() < 0.5 ? new B() : new C() ); }}
^
Why is "java.lang.Object" dropped now? Or, why was it
mentioned in the first case?

And why can I not declare variables of such "&"-types?

You can declare a generic parameter using &. I guess there wasn't
sufficient reason to allow it for variable declarations. I don't know
off hand what kind of further mess to the grammar it would make.
interface A {}
interface B {}

public class Main
{ static void f( final java.lang.String s ){}
public static void main( java.lang.String[] args )
{ A&B a; }}

How are these static types (in parentheses and with "&")
called, which are mentioned in error message, but can not be
used in the source code?

OK, if this was needed, I could write

interface C implements A, B {} (...) C a;

You can write:

<T extends A&B> void fn(T t) {
...
}

(Which will produce a method with erasure void fn(A).)

Tom Hawtin
 
P

P.Hill

Stefan Ram wrote:

If you'd kindly tell us what you are trying to implement
in Java or discover about the language we might better be
able to help you better.

[...]
Here, one can notice that "java.lang.Object" is being
mentioned. If "A_" is removed, one might expect
"java.lang.Object&A" as type, but gets only "A":

In one case A is a common base type which the compiler can use to
identify the type of object returned by your cryptic expression.
The Object&A&_A is a bit of odd notation that the compiler
used to identify the type of the expression in order to say it
didn't have any such method that took such a type, it means
AN object which also implements A and _A. It could have said
A&_A and it might in the next release, I doubt such cryptic notation is
part of any standard.
Why is "java.lang.Object" dropped now? Or, why was it
mentioned in the first case?

Dropped? It doesn't need to mention it. All types derive from
java.lang.Object.
And why can I not declare variables of such "&"-types?

Why would you want to? (see note at top and bottom).
How are these static types (in parentheses and with "&")
called, which are mentioned in error message, but can not be
used in the source code?

interface C implements A, B {} (...) C a;

Yes, if you want to invent a type you ought to give it a name.
Why don't you want to give it a name?

There is use of & in the generic notation (as mentioned in another
reply), but without further information about YOUR goal, I can't
guess what you are trying to accomplish, so I can't help accomplish it,
but I'd bet you are trying to program in another language when you
should try to write in Java.

-Paul
 
I

Ingo R. Homann

Hi,

Roedy said:
The JLS
http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#35470
suggests that interfaces can extend multiple interfaces, just as
classes can implement multiple interfaces.

So you should be able to define an interface C, that extends both A
and B and gives the effect you want without new syntax.

But that does not do what Stefan asked for - the following will not work:

interface A {}
interface B {}
class X implements A, B {}
interface C extends A, B {}

C c=new X(); // error

Of course I'm assuming here, that I cannot change the code in the first
three lines (e.g. 'Comparable', 'Serializable', 'String' or something
like that).

Ciao,
Ingo
 
S

Stefan Ram

P.Hill said:
If you'd kindly tell us what you are trying to implement
in Java or discover about the language we might better be
able to help you better.

What I actually would like to do is to write an expression
that contains a cast of a subexpression to a base class of its
class so that this cast is needed. (I.e., compilation should
fail without the cast, or at least the value of the expression
should change without the cast) This expression should not
require additional declarations (of methods or classes).
An example in Java 1.4 would be

Math.random() < 0.5 ?( java.lang.Object )System.in : System.out

In Java 1.4, the expression will produce a compile-time error
if the cast operator "( java.lang.Object )" is removed.

But in Java 1.5 the cast is not needed anymore.

The observation you responded to was a side-effect in this
search.
 
R

Roedy Green

class X implements A, B {}
interface C extends A, B {}

C c=new X(); // error

so he would have to write
class X implements C
or
class X implements A,B,C
 
I

Ingo R. Homann

Hi Roedy,

As I said before, consider:

A=Comparable
B=Serializable
X=String

Roedy said:
so he would have to write
class X implements C
or
class X implements A,B,C

Unfortunately, you can not do so.

Ciao,
Ingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top