frunk icon indicating copy to clipboard operation
frunk copied to clipboard

HList `try_pluck`

Open whatisaphone opened this issue 1 year ago • 2 comments

I had a go at writing a try_pluck method on HList. It would return Some if the type is in the list, or None if it isn't. But I wasn't able to make it work due to rustc complaining about conflicting impls. My gut tells me this would need specialization on nightly, but I could be wrong about that. Do you have any thoughts/ideas?

whatisaphone avatar Jul 28 '22 14:07 whatisaphone

Hmmmm. Yes I can see why this would raise conflicting impls. The thing that makes pluck possible is that, if an HList contains only one copy of T, then there is exactly one applicable impl.

For try_pluck, there doesn't seem to be any way to write an impl for the None case that would not be applicable to all HLists. This will always lead to a conflict with the "Some" case. (or to a type inference error for the Index type, which could either be the "found" index or a "not found" index (probably the length of the list))

Specialization in rust has had such a tumultuous history that I'm always wary of suggestions that it may help a particular problem. If it does work for this, then it would have to take the place of our index types, since those currently would disambiguate the impls and cause them to not specialize each other. (and getting rid of the index types would be a nice outcome...)

ExpHP avatar Jul 30 '22 17:07 ExpHP

I agree with @ExpHP 's assessment; this will be pretty difficult to implement with HList. I do wonder though : what is the use case for such a method, when HList is statically typed? Would it make sense to use something dynamically typed like the way actix extensions is using TypeId?

lloydmeta avatar Aug 01 '22 06:08 lloydmeta