Inheritance

tronicek at fel.cvut.cz tronicek at fel.cvut.cz
Sat May 17 05:08:25 PDT 2008


Hi Mark,

you set the equal sign between closures and generic interfaces. But  
the way how closures are implemented should not dictate their  
behaivour. It should be contrariwise.

As for the use case, one is type aliasing (user Bulder asked for  
aliasing on my blog). For example, instead of repeating { int, int =>  
int } you can declare interface X extends { int, int => int } and use  
X as closure type.

Example:

     interface I1 extends { int => int } { }

     interface I2 extends { String => String } { }

     static class C1 implements I1, I2 {
          public int invoke(int x) { return x; }
          public String invoke(String s) { return s; }
     }

     static void mInt({ int => int } p) {
         System.out.println(p.invoke(42));
     }

     static void mString({ String => String } p) {
         System.out.println(p.invoke("hi"));
     }

     public static void main(String[] args) {
         C1 p = new C1();
         mInt(p);
         mString(p);
     }

But I definitely do not think this is a crutial feature. So, we would  
better if we move to another topic.

Z.
-- 
Zdenek Tronicek
Department of Computer Science and Engineering
Prague                   tel: +420 2 2435 7410
http://cs.felk.cvut.cz/~tronicek


Quoting Mark Mahieu <mark at twistedbanana.demon.co.uk>:

> Well, the compiler should work the way the specification says it should ;)
>
> Seriously though, while I can see that perhaps the spec could be
> changed to allow this in very specific circumstances - where it's
> possible to unambiguously generate a single bridge method for example
> (?) - I can't think of a convincing use case... do you have one?
>
> Every time I've wanted a single class to implement the same generic
> interface multiple times with different type arguments, the types in
> question have been disjoint.
>
> Or am I missing the point?
>
> Mark
>
>
> On 16 May 2008, at 15:24, tronicek at fel.cvut.cz wrote:
>
>> Hi Mark,
>>
>> this is a perfect answer to question "Why the compiler prototype   
>> does not compile it?". But should the compiler work this way? This   
>> is the question I asked in my previous email.
>>
>> Z.
>> -- 
>> Zdenek Tronicek
>> Department of Computer Science and Engineering
>> Prague                   tel: +420 2 2435 7410
>> http://cs.felk.cvut.cz/~tronicek
>>
>>
>> Quoting Mark Mahieu <mark at twistedbanana.demon.co.uk>:
>>
>>> Hi Zdeněk,
>>>
>>> I think the problem here is that the types { => Integer }, { =>  Number
>>> } and { => Object } are all instances of the same generic  interface
>>> (javax.lang.function.O<R,X>) but with different type  arguments, and
>>> according to the JLS :
>>>
>>>
>>> "A class may not at the same time be a subtype of two interface types
>>> which are different invocations of the same generic interface
>>> (§9.1.2), or an invocation of a generic interface and a raw type
>>> naming that same generic interface.
>>>
>>> ...
>>>
>>> This requirement was introduced in order to support translation by
>>> type erasure (§4.6)."
>>>
>>> (From http://java.sun.com/docs/books/jls/third_edition/html/
>>> classes.html#8.1.5 )
>>>
>>>
>>> Regards,
>>>
>>> Mark
>>>
>>>
>>> On 16 May 2008, at 08:32, tronicek at fel.cvut.cz wrote:
>>>
>>>> Hi,
>>>>
>>>> this is compilable:
>>>>
>>>>   interface J1 {
>>>>       Integer invoke();
>>>>   }
>>>>
>>>>   interface J2 {
>>>>       Number invoke();
>>>>   }
>>>>
>>>>   interface J3 {
>>>>       Object invoke();
>>>>   }
>>>>
>>>>   interface J4 extends J1, J2, J3 { }
>>>>
>>>>   class C3 implements J1, J2, J3 {
>>>>       public Integer invoke() { return null; }
>>>>   }
>>>>
>>>>   class C4 implements J4 {
>>>>       public Integer invoke() { return null; }
>>>>   }
>>>>
>>>> and this is not:
>>>>
>>>>   interface I1 extends { => Integer } { }
>>>>
>>>>   interface I2 extends { => Number } { }
>>>>
>>>>   interface I3 extends { => Object } { }
>>>>
>>>>   interface I4 extends I1, I2, I3 { }
>>>>
>>>>   class C1 implements I1, I2, I3 {
>>>>       public Integer invoke() { return null; }
>>>>   }
>>>>
>>>>   class C2 implements I4 {
>>>>       public Integer invoke() { return null; }
>>>>   }
>>>>
>>>> But probably should be. As well as this:
>>>>
>>>>   interface Simple extends { => Integer }, { => Number }, { =>     
>>>> Object } { }
>>>>
>>>> Zdenek
>>>> -- 
>>>> Zdenek Tronicek
>>>> Department of Computer Science and Engineering
>>>> Prague                   tel: +420 2 2435 7410
>>>> http://cs.felk.cvut.cz/~tronicek
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>
>>
>>
>>







More information about the closures-dev mailing list