Lib.optional considered harmful

This has been bugging me for a while, but I’ve never gotten around to writing something coherent about it. So I’ll just put the idea here since I’m curious if the community agrees.

I don’t like lib.optional. I think its highly counterintuitive that it wraps the result in a list. Here’s an example of an abuse: https://github.com/NixOS/nixpkgs/commit/bbb813279055ee2563054d839ff7a2ee696a9ce7

Can’t we just use lib.optionals instead? Yes, it requires two additional characters ([]) but I think that is well worth the clarity.

5 Likes

generally I use optionals + [ ], even for single elements, just to avoid this scenario. Also makes adding new elements easier on the diffs.

I like the idea.

Btw, stats:

$ rg  "optional " | wc -l
4576
$ rg  "optionals " | wc -l
2460