मैं निम्न विधि जहाँ मैं event.status
संपत्ति का परीक्षण करना चाहते ही अगर status
में पारित किया गया है है:टेस्ट केवल तभी चर में रिक्त नहीं है अगर बयान
def findEvent(String desc, String status = null, Collection events) {
return events.find {
it.description == desc && \\If status is not null: it.status == status
}
throw new Exception("Review Event Record Not Found: ${desc}")
}
मैंने सोचा कि यह इस तरह किया जा सकता है, लेकिन यह काम नहीं कर रहा है:
def findEvent(String desc, String status = null, Collection events) {
return events.find {
it.description == desc && (status != null ?: {it.status == status})
}
throw new Exception("Review Event Record Not Found: ${desc}")
}
क्या ऐसा कोई तरीका हो सकता है? या मुझे इस तरह कुछ वापस जाना है:
if (status != null) {
return events.find {
it.description == desc && it.status == status
}
} else if (status == null) {
return events.find {
it.description == desc
}
}
क्या कोई सर्वोत्तम अभ्यास है?
(it.description == desc) && ((status == null) || (it.status == स्थिति)) सही दिखता है। मैं पुष्टि करता हूं कि जब मुझे मौका मिलता है तो यह काम करता है। मान्य है, यह पहली बार है जब मुझे कभी एल्विस ऑपरेटर का सामना करना पड़ा था, इसलिए मैं अंधेरे में खेल रहा था। चीयर्स, डेव –