Kotlin / api-guidelines

Best practices to consider when writing an API for your library
https://kotl.in/api-guide
Apache License 2.0
145 stars 20 forks source link

The example on avoiding arrays is incorrect because Enum.values() performs a defensive copy. #16

Closed LouisCAD closed 10 months ago

LouisCAD commented 1 year ago

https://github.com/Kotlin/api-guidelines/blob/461bd2b03d7b99bee2c2fe400a36fe5d50a359bf/docs/topics/jvm-api-guidelines-predictability.md?plain=1#L152-L170

qwwdfsad commented 1 year ago

This topic can be rephrased better, indeed.

The underlying point is to avoid arrays, because otherwise authors are forced to make a defensive copy (and that's not that great -- it's hard to reason about in the long run; this is exactly the reason Enum.values() is deprecated for good). We should shift focus to that point rather than "do a defensive copy otherwise"

asm0dey commented 1 year ago

Of course, we meant to say, "If enum would return not a defensive copy, then… And defensive copies might be expensive."