#Narrowing enums

1 messages · Page 1 of 1 (latest)

floral sail
#

I have a general enum with three variants. I then have a function that should only work with two of those variants. It seems I should make a new type with just those two variants, but I can't figure out how to do that without coming up with new names.

Is this possible or is it necessary to come up with new names?

type MainType {
  One
  Two
  Three
}

type NarrowedType {
  One
  Two
}

fn do_something(t: NarrowedType) {
  case t {
    One -> do_one()
    Two -> do_two()
  }
}

error: Duplicate definition
   ┌─ /path/to/src/narrow.gleam:8:3
   │
 2 │   One
   │   ^^^ First defined here
   ·
 8 │   One
   │   ^^^ Redefined here

`One` has been defined multiple times.
Names in a Gleam module must be unique so one will need to be renamed.
lone jungle
#

new names or new modules are ya options

#

or use phantom types to act as a witness for the narrowing and then have a safe _ -> panic pattern inside do_something

floral sail
#

Ok, it's not a biggie, just checking I wasn't missing something. Thank you for your continued help!

lone jungle