in Groovy [] .sum() restituiscenulla quando mi aspettoin Groovy [] .sum() restituisce null quando mi aspetto 0
6
A
risposta
7
Secondo http://jira.codehaus.org/browse/GROOVY-2411 questo è il comportamento previsto come somma() funziona per un array di stringhe. La soluzione è quella di utilizzare [] .sum (0) che restituirà 0.
4
Se si vuole veramente a zero con un elenco vuoto, si può sempre usare:
List foo = []
def bar = foo.sum() ?: 0
assert bar == 0
L'operatore elvis valuterà solo il lato destro se il lato sinistro è nullo.
Problemi correlati
- 1. Perché SUM (...) su un recordset vuoto restituisce NULL anziché 0?
- 2. Quando "metaClass" di Groovy restituisce null
- 3. Math.min.apply restituisce 0 per null
- 4. MessageDigest MD5 Algorithm non restituisce quello che mi aspetto
- 5. Quando SqlCommand.ExecuteReader() restituisce null?
- 6. get_selectedItems() [0] su RadGrid restituisce null
- 7. La mia query SUM Select restituisce null. Esso dovrebbe restituire 0
- 8. Cosa è più veloce: SUM su NULL o su 0?
- 9. Quando ShowDialog() restituisce null?
- 10. Sum() provoca un'eccezione invece di restituire 0 quando nessun file
- 11. Mysql - Il conteggio NULL restituisce sempre 0?
- 12. HttpServletRequest.getSession (false): Quando restituisce null?
- 13. Quando può Process.Start() restituisce null?
- 14. XPath in Nokogiri restituisce array vuoto [] mentre mi aspetto di avere risultati
- 15. Differenza tra ISNULL (SUM (x), 0) o SUM (ISNULL (x, 0) in SQL Server
- 16. WPF - FindName restituisce NULL quando non dovrebbe
- 17. Perché {} + [] restituisce 0 in Javascript?
- 18. Cosa fa JdbcTemplate quando RowMapper restituisce null?
- 19. findViewById restituisce NULL quando si utilizza Fragment
- 20. getResource() di ClassLoader restituisce null quando quota singola nel percorso
- 21. JavaScript - instanceof non fare quello che mi aspetto
- 22. Perché il prefisso di pg_search non funziona come mi aspetto?
- 23. Perché `null> = 0 && null <= 0` ma non` null == 0`?
- 24. CloudBlob.DownloadToStream restituisce null
- 25. Come mi aspetto un errore in un test unitario?
- 26. MySQL Trasmetti NULL all'intero 0
- 27. JSON jsonObject.optString() restituisce String "null"
- 28. UITextfield.text restituisce null
- 29. BitmapFactory.decodeByteArray() restituisce NULL
- 30. "SELECT last_insert_rowid()" restituisce sempre "0"
Questo è vero, e lo stavo facendo in precedenza per aggirare il problema. Tuttavia, .sum (0) mi sembra più pulito – Peter