develooper Front page | perl.perl6.users | Postings from January 2020

Changing the error message from subset type violation

Thread Next
From:
Joseph Brenner
Date:
January 12, 2020 23:31
Subject:
Changing the error message from subset type violation
Message ID:
CAFfgvXXSEKYPxV6C9iUBd+6+-QyUyUPgxXq3Y4sSXr+s4tjX2w@mail.gmail.com
I was just playing around with using a "subset" to restrict a
string to a set of allowed values:

  subset Monster of Str where * eq any( << godzilla mothera blob
tingler grendel minotaur >> );

  my Monster $thingie;
  $thingie = 'grendel';  # accepts this as expected

  $thingie = 'nada';     # fails as expected, with message:
  # Type check failed in assignment to $thingie; expected Monster but
got Str ("nada")

I was wondering if there's some way to replace that error message
with a better one.  If there's only a half dozen allowed values
I'd want to tell the user something like

  "The string nada is note one of the allowed values
   godzilla mothera blob tingler grendel minotaur"

I keep thinking there should be a form of where with a CATCH that
should work, but I don't see how to do it.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About