2011-08-27 19 views
15

मैं इतना बल्कि यह लिखने के लिए करना चाहते हैं:संकलित करें: " '<>' अनाम वर्गों के साथ इस्तेमाल किया नहीं किया जा सकता"

Lists.transform(vals, 
    new Function<>() { 
     public List<ValEntry> apply(Validator<? super T> input) { 
      return input.validate(value); 
     } 
    }); 

... इस से:

Lists.transform(vals, 
    new Function<Validator<? super T>, List<ValEntry>>() { 
     public List<ValEntry> apply(Validator<? super T> input) { 
      return input.validate(value); 
     } 
    }); 

लेकिन जावा कंपाइलर मुझे निम्न त्रुटि संदेश देता है:

'<>' cannot be used with anonymous classes 

क्या इसके लिए कोई मौलिक कारण है? या सिर्फ जेडीके 7 में फीचर को छोड़ दिया, शायद वे इसे 8 में करते हैं?

+1

जावा, स्काला के साथ बेहतर नृत्य ... – Landei

+3

के साथ लड़ाई मत करो कभी कभी जीवन में, आप कोई विकल्प नहीं लड़ने के लिए है, लेकिन । – Lii

+1

मुझे जावा को एक कार्यात्मक भाषा – Woot4Moo

उत्तर

14

project coin documentation के अनुसार:

Internally, a Java compiler operates over a richer set of types than those that can be written down explicitly in a Java program. The compiler-internal types which cannot be written in a Java program are called non-denotable types. Non-denotable types can occur as the result of the inference used by diamond. Therefore, using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change. It is feasible that future platform versions could allow use of diamond when creating an anonymous inner class as long as the inferred type was denotable.

संपादित तो यह एक भविष्य संस्करण में संभव है। यह जावा 8 के साथ अभी भी संभव नहीं है, लेकिन अब हमारे पास भेड़ का बच्चा है, इसलिए इसकी आवश्यकता कम है।

+1

अनुसंधान के लिए धन्यवाद। जेनेरिक और प्रकार अनुमान के आंतरिक दिलचस्प लेकिन जटिल चीजें हैं। – Lii

+4

मुझे अभी भी यह नहीं मिला है। एक कन्स्ट्रक्टर आमंत्रण में कक्षा निकाय क्यों जोड़ता है हीरा ऑपरेटर को टाइप पैरामीटर को घुमाने से अक्षम करता है? 'ArrayList a = new ArrayList <>(); 'कानूनी है लेकिन' ArrayList a = new ArrayList <>() {};' नहीं है। प्रकार पैरामीटर स्पष्ट रूप से 'स्ट्रिंग' है। अनाम प्रकार के लिए क्लास फ़ाइल क्यों नहीं कह सकती है? – thejoshwolfe

+0

जावा 8 के साथ अभी भी संभव नहीं है – gontard

3

यह अब जावा 9. में शामिल किया जाना निर्धारित है JEP 213: Milling Project Coin से:

  1. Allow diamond with anonymous classes if the argument type of the inferred type is denotable . Because the inferred type using diamond with an anonymous class constructor could be outside of the set of types supported by the signature attribute, using diamond with anonymous classes was disallowed in Java SE 7. As noted in the JSR 334 proposed final draft, it would be possible to ease this restriction if the inferred type was denotable.
संबंधित मुद्दे