As I was next to you in today's Raku gathering, I came up with this as a
sort-of answer. Not very happy with it but it gets partway there.

#!/usr/bin/env perl6
use v6.d;

## Simple version to check membership, but the type error won't tell which
strings
# subset Critter of Str where any <ant bee cat dog elephant>

## This subset knows its strings and notes the right values. But, can't
catch that message.
subset Critter of Str where {
my @valid-animals = <ant bee cat dog elephant>;

# What I'd like to do here, is return a block which accepts the
# right things, and adds to the type check failure on bad things.
# But all I can get working, is checking & returning a boolean,
# perhaps noting a message. The caller then ACCEPTS against either
# True or False and doesn't know about the note.

$_ ~~ any @valid-animals
 or {
note("at $^c.file():$^c.line() Critter must be one of @valid-animals[]");
False;
 }(callframe(4)); # fiddle with callframe until Raku gets Carp
};

my Critter $pet = 'bee';
my Critter $pat = 'fish';

CATCH {
default {
note "** Caught this **";
.rethrow
}
}

##   Ideal output
# ** Caught this **
# Type check failed in assignment to $pat; expected Critter but got Str
("fish")
# Critter must be one of ant bee cat dog elephant
#   in block <unit> at ./substr_err.raku line 27

##   Actual output
# at ./subset_except.raku:27 Critter must be one of ant bee cat dog elephant
# ** Caught this **
# Type check failed in assignment to $pat; expected Critter but got Str
("fish")
#   in block <unit> at ./subset_except.raku line 27

Reply via email to