Undenotable types in Java

31 views
Skip to first unread message

Zhong Yu

unread,
Oct 6, 2015, 12:04:48 AM10/6/15
to java-lang-fans
Types that we cannot reference by name in our code; however, the compiler internally has names for them; and programmers also need to assign some names when reasoning about these types.

- null type

- intersection type

- infinite type from lub()  (jls#4.10.4)

- type variable introduced by capture conversion

- anonymous class ( fun examples: http://stackoverflow.com/a/32960669)

any more?

Zhong Yu
bayou.io

Zhong Yu

unread,
Oct 6, 2015, 12:18:09 AM10/6/15
to java-lang-fans
To simulate implicitly typed variable in C#

    var list = new ArrayList<String>();

which is very important for undenotable types, we may do

    with( new ArrayList<String>(), list->{
        ...
    });

It's not the same thing though of course :(

Zhong

y s

unread,
Oct 6, 2015, 10:45:00 AM10/6/15
to Zhong Yu, java-lang-fans
If you're counting anonymous classes, then I think you could also count the classes created for lambdas and method references.

--
You received this message because you are subscribed to the Google Groups "java.lang.fans" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-lang-fan...@googlegroups.com.
To post to this group, send email to java-la...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/java-lang-fans/CACuKZqHaDrcpR_W2FcmcGmmH6KjtSroA_32yQJjNEHijU3nn5Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Zhong Yu

unread,
Oct 6, 2015, 4:40:14 PM10/6/15
to y s, java-lang-fans
On Tue, Oct 6, 2015 at 9:44 AM, y s <eleus...@gmail.com> wrote:
If you're counting anonymous classes, then I think you could also count the classes created for lambdas and method references.



I believe that is different.

A lambda expression's static type is a functional interface, based on the target type. (jls#15.27.3). Although at runtime the expression must evaluates to an object of a concrete subclass, the compiler does not know or care about the subclass. That subclass is not part of the type system at compile time.

An anonymous class instantiation expression declares a subclass, which *is* the static type of the expression. jls#15.9 --

> Such ... expression declares an anonymous class and creates an instance of it

> The type of the class instance creation expression is the return type of the chosen constructor ... the type of the class instance creation expression may be an anonymous class type

> anonymous class declaration is automatically derived ... by the Java compiler

To the compiler, the subclass is not much different from other classes; it just lacks an official name that the programmer can reference.

We can probe the compiler by this illegal statement

        Class<Object> c = new Object(){}.getClass();

Error: incompatible types: java.lang.Class<capture#1 of ? extends <anonymous java.lang.Object>> cannot be converted to java.lang.Class<java.lang.Object>


Zhong
 

Zhong Yu

unread,
Oct 6, 2015, 4:43:08 PM10/6/15
to y s, java-lang-fans

On Tue, Oct 6, 2015 at 3:40 PM, Zhong Yu <zhong...@gmail.com> wrote:

We can probe the compiler by this illegal statement

        String s = new Object(){};

Error: incompatible types: <anonymous java.lang.Object> cannot be converted to java.lang.String

y s

unread,
Oct 6, 2015, 5:20:34 PM10/6/15
to Zhong Yu, java-lang-fans
Yeah, that's a good point. I wouldn't be surprised if the compiler did internally have a name for lambdas, but it doesn't leak it in any construct I could come up with. I think I mistook your "compiler has an internal name for" as meaning "javac might have some name for the type of this construct," and not "the name interacts with a user of javac in some way."
Reply all
Reply to author
Forward
0 new messages