`case_when` stripping list keynames

Hi all,

When I attempt to return a named list from case_when, it strips the names from the returned list:


dplyr::case_when(
  TRUE ~ list(something = 1, another = 2)
)
#> $<NA>
#> [1] 1
#> 
#> $<NA>
#> [1] 2

list(something = 1, another = 2)
#> $something
#> [1] 1
#> 
#> $another
#> [1] 2

Is this intended functionality or is this a bug?

Thanks

In my opinion its neither intended nor a bug.
I think case_when is designed to return outputs of consistent type, it will throw explicit errors if the user tries to mix types, having the type in question be a list, is a big of a dodge because inside the list could be anything, so to me its a case of case_when not being designed with the intent of giving complete freedom of return values, and you've found a case where probably case_when should benefit from a minor modification to throw an explicit error message about being asked to return lists at all.
I'm just speaking for myself as a user and am not affiliated at all with the dplyr development.

You could potentially bring this to the dplyr github issues page if its important to you as a feature, or to get the gospel on why they wouldn't put it in.

Thanks Nir, I think you're right that type stability is tricky to establish when it comes to lists where the actual desired type of the output isn't clear.

I'll add an issue to Github and see what they say.