3 ms·
> In Rust, pattern matching breaks down as soon as your IR contains pointers to its children > Another way this manifests is that it’s often frustrating that p
by siraben 6y ago
> In Rust, pattern matching breaks down as soon as your IR contains pointers to its children
> Another way this manifests is that it’s often frustrating that patterns aren’t first-class; I can’t pass around a
pattern, or build up a pattern on the fly.
To me this seems like limitations of pattern matching in Rust that perhaps is addressed by Haskell's ViewPatterns language extension. The idea is that the usual ML-style pattern matching exposes too much implementation detail of the ADT, for instance, consider instead the use of a "view function" viewer to convert the structure into a initial segment xs and a last element x.
pattern (:|>) :: Seq a -> a -> Seq a
pattern xs :|> x <- (viewr -> xs :> x)
where
xs :|> x = xs |> x
lastElem :: Seq a -> a
lastElem (_ :|> e) = e
:|> would let one treat the sequence data type as abstract (it could be a finger tree[0] underneath, for instance). In the author's use case of representing IRs, one could move the unboxing work into the view.
[0] https://en.wikipedia.org/wiki/Finger_tree https://en.wikipedia.org/wiki/Finger_tree