#Issue with opaque types and dynamic dispatch question

9 messages · Page 1 of 1 (latest)

lyric hinge
#

Heya everyone, I'm having an issue with opaque types/not fully understanding how to get around an issue I'm having. (also not even fully sure if I understand the issue)

for the example I have 3 functions that return a type of impl Iterator<Item = &'a u32> but when I try to use them all with a match statement I get an error saying my match branches are incompatible since expected opaque type, found a different opaque type because note: distinct uses of 'impl Trait' result in different opaque types

this first link is the non-working code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=97046c19ead967cd50dc935717e64dec
this next bit of code works, however using Box<dyn>/dynamic dispatch is probably not the best idea? I'm not sure how drastic the penalties are for it, but I think they're bad (in my real application the chains are much larger, multiple levels deep, etc, perhaps it is the best solution in my real case?, if someone happens to know/have any resources on dispatching I'd love to see them): https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3ccbae7e26a419227c74910dd065a574

One solution i found was https://rust-lang.github.io/impl-trait-initiative/explainer/tait.html but it's nightly and i would like to stay on a stable branch

Thank you for any suggestions in advance, have a great day

south terrace
#

dynamic dispatch is usually fine

#

it really depends on how much you're using it and how exactly you're using it

#

you could also use the either crate

#

which is just an enum

#

so no dynamic dispatch

lyric hinge
#

it's going to be pretty heavily used, it's working on a chess engine and this is part of calculating valid moves for each piece type (the match) with each pattern being piecetype::pawn = moves.get_valid_pawn_moves() and so on

#

i'll look into that, thank you!