Crell / enum-comparison

A comparison of enumerations and similar features in different languages
82 stars 7 forks source link

Handle failure in from #64

Closed iluuu1994 closed 3 years ago

iluuu1994 commented 3 years ago

Some people have suggested either

  1. Adding a has method to know if from will succeed
  2. Make from return null instead

I'm not super happy with either of those but we'll probably have to settle for one of them. @Crell Which do you prefer?

/cc @bwoebi

bwoebi commented 3 years ago

I wouldn't do 2. - often enough your inputs come from a trusted source, it's good to have a hard failure there instead of passing null around.

Adding has is fine.

iluuu1994 commented 3 years ago

@Crell I'm ok with option 1 if you are.

Crell commented 3 years ago

I'd favor 1. Forcing the user to check for null every time to avoid random nulls elsewhere is a bad idea.

has-and-exception it is.

iluuu1994 commented 3 years ago

Not yet specified in the RFC. @Crell Can you do that?

Crell commented 3 years ago

Added has().

Should we throw a more specific error for a missing case than just ValueError? Something more easily catchable, like EnumCaseNotFoundError, which includes the bad value that was requested?

bwoebi commented 3 years ago

I feel like ValueError is appropriate - don't catch, use has() would be my philosophy. Better for the ecosystem if there are not multiple obvious ways to do the same thing.

iluuu1994 commented 3 years ago

I think ValueError works fine, since that is the only exception thrown by this functions. Thanks!