lloydmeta / frunk

Funktional generic type-level programming in Rust: HList, Coproduct, Generic, LabelledGeneric, Validated, Monoid and friends.
https://beachape.com/frunk/
MIT License
1.24k stars 56 forks source link

HList `try_pluck` #197

Open whatisaphone opened 2 years ago

whatisaphone commented 2 years ago

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?

ExpHP commented 2 years ago

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...)

lloydmeta commented 2 years ago

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?