CLOVIS
03/10/2026, 8:00 AMStefan Oltmann
03/10/2026, 8:31 AM@Deprecated in new code, but the standard solution isn’t very elegant either. If I'd do it that way I would leave that annotation out.
Maybe you’re right: There probably should be a mandatory varargs language feature.
That said, I don’t use varargs very often. I usually prefer a proper Set or List.Tgo1014
03/10/2026, 8:55 AMCLOVIS
03/10/2026, 9:04 AMSet or List.
That's another article that's coming soon, but yeah varargs should only ever be used as a convenience overload for the real method that takes a Set or List (because the spread operator is not very good)Laurent Thiebaud
03/10/2026, 11:25 AMCLOVIS
03/10/2026, 1:18 PMNonEmptyList cannot be used as a vararg. Its constructor uses the (e: T, es: List<T>) version that I showcase in the first half of the article.Frank Bouwens
03/10/2026, 1:48 PMTodd
03/10/2026, 5:47 PMCLOVIS
03/10/2026, 7:25 PMphldavies
03/10/2026, 8:00 PM.containsAll(elements = emptyArray()) or .containsAll(*emptyArray()) but that's fine for some cases (like your example) - but wouldn't work for something like NonEmptyListCLOVIS
03/10/2026, 8:02 PMNonEmptyList, there is no other choice than a runtime exception because it's not possible to know at compile-time that the input list is empty—that's why NonEmptyList exists, after all!phldavies
03/10/2026, 8:07 PMNonEmptyList that's very likely why it has the classic T, vararg T parameters to enforce at compile time that construction is with at least one value. The conversions from Iterable etc to NonEmptyList require explicitly handling (or will explicitly throw).CLOVIS
03/10/2026, 8:09 PMphldavies
03/10/2026, 8:09 PMCLOVIS
03/10/2026, 8:09 PMNonEmptyList , I probably would still use the deprecated trick + a runtime check.CLOVIS
03/10/2026, 8:09 PM