tcr / corollary

Cross-compiler from Haskell to Rust, plus parser-haskell.
73 stars 5 forks source link

How can you match on an empty array? #33

Open tcr opened 7 years ago

tcr commented 7 years ago

Say we're matching:

case value of 
    Container([]) => ...,
    Container(vector) => ...,

This won't work in Rust if you try to match against a container. Unless box [] pattern works. Does this need an extensive AST transform to work?

jdm commented 7 years ago

Would slice patterns in nightly rustc work? https://doc.rust-lang.org/book/slice-patterns.html

jdm commented 7 years ago

Alternatively, use a pattern guard in a match?

tcr commented 7 years ago

Pattern guards are a good choice here, I think the pattern is just [] and non_empty_vec throughout the code!

Slice patterns would work, though only at the top-level. Pattern matching against Vectors and Strings is not possible unless they're converted into boxed objects first (and box_patterns is enabled), so I should get more data on how they're used.