Matching on constants in a bitstring case expression #2333
-
First of all, amazing work! I'm loving everything in the language so far. One thing that came up while I was trying to work on a jpeg parser was trying to give clear names to bitstring patterns I want to match on. I was able to get something working with fn parse_segment(marker: BitString, bytes: BitString) {
case marker {
<<0xFF, 0xE:4, app_number:4>> -> {
app.parse(bytes, app_number)
}
<<0xFF, 0xC:4, identifier:4>> -> {
frame.parse(bytes, identifier)
}
}
} But what I wanted was something more along the lines of const marker_prefix = 0xFF
const app_marker = 0xE
const frame_marker = 0xC
fn parse_segment(marker: BitString, bytes: BitString) {
case marker {
<<marker_prefix, app_marker:4, app_number:4>> -> {
app.parse(bytes, app_number)
}
<<marker_prefix, frame_marker:4, identifier:4>> -> {
frame.parse(bytes, identifier)
}
}
} but when I do that, the In elixir the ambiguity between the two isn't an issue when using module attributes so maybe this isn't such a simple problem to solve |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello! In Gleam we don't have one syntax for both equality and also for assigning variables, so instead guards are used. <<m, a:4, f:4>> if m == marker_prefix && a == app_marker && f == frame_marker -> todo |
Beta Was this translation helpful? Give feedback.
Hello! In Gleam we don't have one syntax for both equality and also for assigning variables, so instead guards are used.