Stream: beginners

Topic: exhaustive pattern matching on U8?


view this post on Zulip Matthieu Pizenberg (May 30 2024 at 16:32):

I’m implementing a non linear function from U8 to U8. I could use a list of 256 elements and use List.get but the never-existing errors might be annoying so I tried just writing a huge when n is ... with all integers from 0 to 255.

when n is
  0 -> 99
  1 -> 124
  ...
  255 -> 22

But the compiler tells me this:

330│>          254 -> 187
331│>          255 -> 22

Other possibilities include:

    _

I would have to crash if I saw one of those! Add branches for them!

view this post on Zulip Matthieu Pizenberg (May 30 2024 at 16:33):

I should add that I put a type annotation:

sbox : U8 -> U8
sbox = \i ->
    when i is
        0 -> 99
        1 -> 124
  ...

view this post on Zulip Matthieu Pizenberg (May 30 2024 at 20:50):

Is it expected that the type inference is not aware that 0..255 covers all numbers in U8? or is it a bug?

view this post on Zulip Brendan Hansknecht (May 30 2024 at 21:01):

I would guess a bug

view this post on Zulip Richard Feldman (May 30 2024 at 23:21):

I think we just never implemented it!


Last updated: Jul 06 2025 at 12:14 UTC